diff --git a/app/assets/stylesheets/spree/backend/spree_digital_assets.css b/app/assets/stylesheets/spree/backend/spree_digital_assets.css
index c7b0692..8fed327 100644
--- a/app/assets/stylesheets/spree/backend/spree_digital_assets.css
+++ b/app/assets/stylesheets/spree/backend/spree_digital_assets.css
@@ -3,3 +3,7 @@ Placeholder manifest file.
the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/backend/all.css'
*= require spree/backend/spree_digital_assets/style
*/
+
+.note {
+ font-size: 10px;
+}
diff --git a/app/controllers/spree/admin/banners_controller.rb b/app/controllers/spree/admin/banners_controller.rb
index 705303d..c300fe3 100644
--- a/app/controllers/spree/admin/banners_controller.rb
+++ b/app/controllers/spree/admin/banners_controller.rb
@@ -1,6 +1,5 @@
module Spree
module Admin
-
class BannersController < ResourceController
before_action :load_banner, only: [:edit, :update, :destroy, :toggle_banner_active_status]
@@ -22,10 +21,10 @@ def index
end
def toggle_banner_active_status
- if @banner.toggle!(:active)
+ if @banner.change_active_status
flash[:success] = Spree.t(:successfully_updated_banner)
else
- flash[:danger] = Spree.t(:error)
+ flash[:notice] = @banner.errors.full_messages.join
end
redirect_to admin_banners_path
end
diff --git a/app/models/spree/banner.rb b/app/models/spree/banner.rb
index 3161b05..1ac0eb5 100644
--- a/app/models/spree/banner.rb
+++ b/app/models/spree/banner.rb
@@ -1,11 +1,15 @@
module Spree
class Banner < Spree::Base
+
+ URL_VALIDATION_REGEX = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
+
has_attached_file :attachment, styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>' },
url: '/spree/banner/:id/:style/:basename.:extension',
path: ':rails_root/public/spree/banner/:id/:style/:basename.:extension'
+
validates_attachment :attachment, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
- validate :only_one_mobile_banner, if: :mobile_banner?
+ validate :only_one_mobile_banner
with_options presence: true do
validates :title, uniqueness: true
@@ -13,14 +17,43 @@ class Banner < Spree::Base
validates :attachment
end
+ validates_format_of :link, with: URL_VALIDATION_REGEX, multiline: true, allow_blank: true
+
scope :active, -> { where(active: true) }
- scope :mobile_banner, -> { where(mobile_banner: true) }
+ scope :mobile_active_banner, -> { where(mobile_banner: true, active: true) }
+
+ before_destroy :restrict_if_active
- def only_one_mobile_banner
- if Banner.mobile_banner.present?
- errors.add(:mobile_banner, Spree.t(:only_one_mobile_banner))
+ def change_active_status
+ if active?
+ update_columns(active: false, mobile_banner: false)
+ else
+ update_column(:active, true)
end
end
+ private
+
+ def only_one_mobile_banner
+ if mobile_banner?
+ if !active?
+ errors.add(:active, Spree.t(:only_active_mobile_banner))
+ elsif other_mobile_banner_active?
+ errors.add(:base, Spree.t(:only_one_mobile_banner))
+ end
+ end
+ end
+
+ def other_mobile_banner_active?
+ (Banner.mobile_active_banner - [self]).length > 0
+ end
+
+ def restrict_if_active
+ if active?
+ errors.add(:base, Spree.t(:cannot_delete_active_banner))
+ throw(:abort)
+ end
+ end
+
end
end
diff --git a/app/views/spree/admin/banners/_form.html.erb b/app/views/spree/admin/banners/_form.html.erb
index ed07ede..d494a2e 100644
--- a/app/views/spree/admin/banners/_form.html.erb
+++ b/app/views/spree/admin/banners/_form.html.erb
@@ -3,14 +3,12 @@
<%= f.field_container :title do %>
<%= f.label :title %> *
<%= f.text_field :title, class: 'form-control' %>
- <%= f.error_message_on :title %>
<% end %>
Upload pic with approximate 1022 × 60 dimensions
<% end %> @@ -33,7 +31,6 @@ <%= f.field_container :mobile_banner do %> <%= f.label :mobile_banner %> <%= f.check_box :mobile_banner %> - <%= f.error_message_on :mobile_banner %> <% end %> diff --git a/app/views/spree/admin/banners/edit.html.erb b/app/views/spree/admin/banners/edit.html.erb index 6f188dd..02425a6 100644 --- a/app/views/spree/admin/banners/edit.html.erb +++ b/app/views/spree/admin/banners/edit.html.erb @@ -6,6 +6,8 @@ <%= button_link_to Spree.t(:back_to_banner_list), spree.admin_banners_path, icon: 'arrow-left', class: 'btn btn-primary' %> <% end %> +<%= render partial: 'spree/shared/error_messages', locals: { target: @banner } %> + <%= form_for [:admin, @banner] do |f| %>