Skip to content

Commit

Permalink
asset box added
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshumishra31 committed Aug 28, 2018
1 parent adb73bc commit a08a00a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 38 deletions.
15 changes: 14 additions & 1 deletion app/controllers/spree/admin/banners_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module Admin
class BannersController < ResourceController

before_action :load_banner, only: [:edit, :update, :destroy, :toggle_banner_active_status]
before_action :load_folders, only: [:new, :create, :edit, :update]
before_action :create_banner_and_images, only: [:create]

def create
@banner = Banner.new(permitted_params)
if @banner.save
flash[:success] = Spree.t(:successfully_created_banner)
redirect_to admin_banners_path
Expand Down Expand Up @@ -40,6 +41,18 @@ def update

private

def create_banner_and_images
@banner = Banner.new(permitted_params)
if permitted_resource_params[:images][:digital_asset_id].present?
@banner.images << Spree::Image.new(permitted_resource_params[:images])
end
end

def load_folders
@folders = Spree::Folder.all
@digital_assets = Spree::DigitalAsset.approved.page(params[:page])
end

def permitted_params
params.require(:banner).permit(:title, :attachment, :link, :active, :mobile_banner)
end
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/spree/admin/base_helper_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def asset_details(digital_asset)
def related_products(digital_asset)
products = {}
digital_asset.assets.each do |asset|
product = asset.viewable.product
products[product.id] = { slug: product.slug, name: product.name }
product = asset.viewable.try(:product)
products[product.id] = { slug: product.slug, name: product.name } if product
end
products.values
end
Expand Down
5 changes: 4 additions & 1 deletion app/models/spree/banner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class Banner < Spree::Base

validates_attachment :attachment, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
validate :only_one_mobile_banner
has_many :images, -> { order(:position) }, as: :viewable, dependent: :destroy, class_name: 'Spree::Image'

with_options presence: true do
validates :title, uniqueness: true
validates :link
validates :attachment
validates :attachment, unless: :images
validates :images, unless: :attachment
end

validates_format_of :link, with: URL_VALIDATION_REGEX, multiline: true, allow_blank: true
Expand All @@ -24,6 +26,7 @@ class Banner < Spree::Base

before_destroy :restrict_if_active


def change_active_status
if active?
update_columns(active: false, mobile_banner: false)
Expand Down
5 changes: 4 additions & 1 deletion app/views/spree/admin/banners/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
<% end %>
</div>

<div class="col-md-5">
<div class="col-md-5" data-hook='file'>
<%= f.field_container :attachment do %>
<%= f.label :attachment %><span class="required">*</span><br/>
<%= f.fields_for :images, @banner.images.build do |form| %>
<%= render partial: "spree/admin/images/upload_digital_asset", locals: { f: form } %>
<% end %>
<%= f.file_field :attachment, class: 'form-control select-file btn btn-default' %>
<p class="note text-muted">Upload pic with approximate 1022 × 60 dimensions</p>
<% end %>
Expand Down
76 changes: 43 additions & 33 deletions app/views/spree/admin/banners/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,50 @@
<%= button_link_to Spree.t(:new_banner), spree.new_admin_banner_path, icon: 'arrow-left', class: 'btn btn-primary' %>
<% end %>


<table class="index table table-hover">
<thead>
<tr>
<th><%= Spree.t(:Title) %></th>
<th><%= Spree.t(:Link) %></th>
<th><%= Spree.t(:image) %></th>
<th><%= Spree.t(:active) %></th>
<th><%= Spree.t(:mobile_banner) %></th>
<th class="actions" width="100"></th>
</tr>
</thead>
<tbody>
<% @banners.each do |banner| %>
<tr id="<%= spree_dom_id banner %>" data-hook="admin_banners_index_rows">
<td><%= banner.title %></td>
<td><%= link_to banner.link, banner.link, target: :_blank %></td>
<td><%= image_tag banner.attachment(:mini) %></td>
<td><%= link_to toggle_banner_active_status_admin_banner_path(banner), method: :patch, class: "btn #{banner_active(banner)} btn-site-primary text-uppercase" do %>
<span class='glyphicon glyphicon-pencil'> <%= banner.active %> </span>
<% end %>
</td>
<td><%= banner.mobile_banner %></td>
<td class="actions">
<%= link_to_edit banner, no_text: true if can? :edit, banner %>
<% if banner.active? %>
<%= link_to_with_icon('delete', '', '#', disabled: true, no_text: true, class: 'btn btn-danger btn-sm') %>
<% if @banners.present? %>
<table class="index table table-hover">
<thead>
<tr>
<th><%= Spree.t(:Title) %></th>
<th><%= Spree.t(:Link) %></th>
<th><%= Spree.t(:image) %></th>
<th><%= Spree.t(:active) %></th>
<th><%= Spree.t(:mobile_banner) %></th>
<th class="actions" width="100"></th>
</tr>
</thead>
<tbody>
<% @banners.each do |banner| %>
<tr id="<%= spree_dom_id banner %>" data-hook="admin_banners_index_rows">
<td><%= banner.title %></td>
<td><%= link_to banner.link, banner.link, target: :_blank %></td>
<% if banner.attachment.present? %>
<td><%= image_tag banner.attachment(:mini) %></td>
<% else %>
<%= link_to_delete banner, no_text: true %>
<td><%= image_tag banner.images.first.attachment(:mini) %></td>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<td><%= link_to toggle_banner_active_status_admin_banner_path(banner), method: :patch, class: "btn #{banner_active(banner)} btn-site-primary text-uppercase" do %>
<span class='glyphicon glyphicon-pencil'> <%= banner.active %> </span>
<% end %>
</td>
<td><%= banner.mobile_banner %></td>
<td class="actions">
<%= link_to_edit banner, no_text: true if can? :edit, banner %>
<% if banner.active? %>
<%= link_to_with_icon('delete', '', '#', disabled: true, no_text: true, class: 'btn btn-danger btn-sm') %>
<% else %>
<%= link_to_delete banner, no_text: true %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<div class="alert alert-info no-objects-found">
<%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Banner)) %>,
<%= link_to Spree.t(:add_one), new_object_url if can?(:create, Spree::Banner) %>!
</div>
<% end %>

<%= paginate @banners %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
resources :folders, only: [:create, :update, :destroy]
resources :banners, except: [:show] do
patch :toggle_banner_active_status, on: :member
resources :images
end
end

Expand Down

0 comments on commit a08a00a

Please sign in to comment.