diff --git a/app/controllers/spree/admin/banners_controller.rb b/app/controllers/spree/admin/banners_controller.rb index c300fe3..16b2a54 100644 --- a/app/controllers/spree/admin/banners_controller.rb +++ b/app/controllers/spree/admin/banners_controller.rb @@ -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 @@ -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 diff --git a/app/helpers/spree/admin/base_helper_decorator.rb b/app/helpers/spree/admin/base_helper_decorator.rb index 9abc420..f4393e1 100644 --- a/app/helpers/spree/admin/base_helper_decorator.rb +++ b/app/helpers/spree/admin/base_helper_decorator.rb @@ -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 diff --git a/app/models/spree/banner.rb b/app/models/spree/banner.rb index 1ac0eb5..95196ae 100644 --- a/app/models/spree/banner.rb +++ b/app/models/spree/banner.rb @@ -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 @@ -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) diff --git a/app/views/spree/admin/banners/_form.html.erb b/app/views/spree/admin/banners/_form.html.erb index d494a2e..e476c51 100644 --- a/app/views/spree/admin/banners/_form.html.erb +++ b/app/views/spree/admin/banners/_form.html.erb @@ -12,9 +12,12 @@ <% end %> -
Upload pic with approximate 1022 × 60 dimensions
<% end %> diff --git a/app/views/spree/admin/banners/index.html.erb b/app/views/spree/admin/banners/index.html.erb index 138bf19..3d15af0 100644 --- a/app/views/spree/admin/banners/index.html.erb +++ b/app/views/spree/admin/banners/index.html.erb @@ -6,40 +6,50 @@ <%= button_link_to Spree.t(:new_banner), spree.new_admin_banner_path, icon: 'arrow-left', class: 'btn btn-primary' %> <% end %> - -<%= Spree.t(:Title) %> | -<%= Spree.t(:Link) %> | -<%= Spree.t(:image) %> | -<%= Spree.t(:active) %> | -<%= Spree.t(:mobile_banner) %> | -- | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
<%= banner.title %> | -<%= link_to banner.link, banner.link, target: :_blank %> | -<%= image_tag banner.attachment(:mini) %> | -<%= link_to toggle_banner_active_status_admin_banner_path(banner), method: :patch, class: "btn #{banner_active(banner)} btn-site-primary text-uppercase" do %> - <%= banner.active %> - <% end %> - | -<%= banner.mobile_banner %> | -
- <%= 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? %>
+
| <%= link_to toggle_banner_active_status_admin_banner_path(banner), method: :patch, class: "btn #{banner_active(banner)} btn-site-primary text-uppercase" do %> + <%= banner.active %> + <% end %> + | +<%= banner.mobile_banner %> | ++ <%= 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 %> + | +