Permalink
Browse files

Merge pull request #8 from cantierecreativo/2-0-stable

Minor updates
  • Loading branch information...
2 parents c31e421 + fa00fb4 commit 79b0bcf94f4662c7ea46b3a7cda9436505356fdd @damianogiacomello committed Nov 6, 2013
View
@@ -1,35 +1,56 @@
-SpreeBanner
-===================
+Spree Banner
+============
-Add banner for Spree Commerce Shop [compatible with Amazon S3]
+Adds a way to manage banners to Spree Commerce [compatible with Amazon S3]
-Basic Installation
-------------------
+Installation
+------------
1. Add the following to your Gemfile
-<pre>
- gem 'spree_banner', '~> 2.0.0'
-</pre>
+
+```ruby
+gem 'spree_banner', '~> 2.0.0'
+```
+
2. Run `bundle install`
+
3. To copy and apply migrations run:
-<pre>
- rails g spree_banner:install
-</pre>
-
-Example
-=======
-
-1. add banner helper method in your view:
-<pre>
- <%= insert_banner_box(:category => "my_category") %>
-</pre>
-and add banner in the admin section
+
+```
+rails g spree_banner:install
+```
+
+
+Configuration
+-------------
+
+Preferences can be updated within the admin panel under "Configuration" then "Banner box settings".
+Or you may set them with an initializer within your application:
+
+```ruby
+SpreeBanner::Config.tap do |config|
+ config.banner_styles = {"mini" => "48x48>", "small" => "100x100>", "large" => "800x200#"}.to_json.to_s
+ config.banner_default_style = 'small'
+end
+```
+
+
+Banner use example
+------------------
+
+1. Add banner helper method in your view:
+
+```erb
+<%= insert_banner_box(category: "my_category") %>
+```
+
2. Additional options:
-<pre>
- :class => "my_class"
- :style => "my_style"
- :list = true|false
-</pre>
+
+```ruby
+ :class => "my_class"
+ :style => "my_style"
+ :list = true|false
+```
Copyright (c) 2012 [Damiano Giacomello], released under the New BSD License
@@ -1,25 +1,25 @@
module Spree
module Admin
class BannerBoxSettingsController < Spree::Admin::BaseController
-
+
def show
redirect_to( :action => :edit )
end
def create
update_paperclip_settings
- @styles = ActiveSupport::JSON.decode(Spree::Config[:banner_styles])
+ @styles = ActiveSupport::JSON.decode(SpreeBanner::Config[:banner_styles])
super
end
def edit
- @styles = ActiveSupport::JSON.decode(Spree::Config[:banner_styles])
+ @styles = ActiveSupport::JSON.decode(SpreeBanner::Config[:banner_styles])
end
def update
update_styles(params)
- Spree::Config.set(params[:preferences])
+ SpreeBanner::Config.set(params[:preferences])
update_paperclip_settings
respond_to do |format|
@@ -40,20 +40,19 @@ def update_styles(params)
styles = params[:banner_styles]
- Spree::Config[:banner_styles] = ActiveSupport::JSON.encode(styles) unless styles.nil?
+ SpreeBanner::Config[:banner_styles] = ActiveSupport::JSON.encode(styles) unless styles.nil?
end
def update_paperclip_settings
extended_hash = {}
- ActiveSupport::JSON.decode(Spree::Config[:banner_styles]).each do |key,value|
+ ActiveSupport::JSON.decode(SpreeBanner::Config[:banner_styles]).each do |key,value|
extended_hash[:"#{key}"] = value
end
Spree::BannerBox.attachment_definitions[:attachment][:styles] = extended_hash
- Spree::BannerBox.attachment_definitions[:attachment][:path] = Spree::Config[:banner_path]
- Spree::BannerBox.attachment_definitions[:attachment][:default_url] = Spree::Config[:banner_default_url]
- Spree::BannerBox.attachment_definitions[:attachment][:default_style] = Spree::Config[:banner_default_style]
+ Spree::BannerBox.attachment_definitions[:attachment][:path] = SpreeBanner::Config[:banner_path]
+ Spree::BannerBox.attachment_definitions[:attachment][:default_url] = SpreeBanner::Config[:banner_default_url]
+ Spree::BannerBox.attachment_definitions[:attachment][:default_style] = SpreeBanner::Config[:banner_default_style]
end
end
end
end
-
@@ -39,7 +39,7 @@ def location_after_save
def collection
return @collection if @collection.present?
params[:q] ||= {}
- params[:q][:s] ||= "title asc"
+ params[:q][:s] ||= "category, position asc"
@search = super.ransack(params[:q])
@collection = @search.result.page(params[:page]).per(Spree::Config[:admin_products_per_page])
@@ -4,21 +4,33 @@ module BannerBoxesHelper
def insert_banner_box(params={})
params[:category] ||= "home"
params[:class] ||= "banner"
- params[:style] ||= "small"
+ params[:style] ||= SpreeBanner::Config[:banner_default_style]
params[:list] ||= false
- @@banner = Spree::BannerBox.enable(params[:category])
- if @@banner.blank?
- return ''
- end
- res = []
- banner = @@banner.sort_by { |ban| ban.position }
-
- if (params[:list])
- content_tag(:ul, banner.map{|ban| content_tag(:li, link_to(image_tag(ban.attachment.url(params[:style].to_sym)), (ban.url.blank? ? "javascript: void(0)" : ban.url)), :class => params[:class])}.join().html_safe )
+ banners = Spree::BannerBox.enabled(params[:category]).order(:position)
+ return '' if banners.empty?
+
+ if params[:list]
+ content_tag :ul do
+ banners.map do |ban|
+ content_tag :li, :class => params[:class] do
+ link_to (ban.url.blank? ? "javascript: void(0)" : ban.url) do
+ src = ban.attachment.url(params[:style].to_sym)
+ image_tag(src, :alt => ban.alt_text.presence || image_alt(src))
+ end
+ end
+ end.join.html_safe
+ end
else
- banner.map{|ban| content_tag(:div, link_to(image_tag(ban.attachment.url(params[:style].to_sym)), (ban.url.blank? ? "javascript: void(0)" : ban.url)), :class => params[:class])}.join().html_safe
+ banners.map do |ban|
+ content_tag :div, :class => params[:class] do
+ link_to (ban.url.blank? ? "javascript: void(0)" : ban.url) do
+ src = ban.attachment.url(params[:style].to_sym)
+ image_tag(ban.attachment.url(params[:style].to_sym), :alt => ban.alt_text.presence || image_alt(src))
+ end
+ end
+ end.join.html_safe
end
end
-
+
end
end
@@ -1,25 +0,0 @@
-# This is the primary location for defining spree preferences
-#
-# The expectation is that this is created once and stored in
-# the spree environment
-#
-# setters:
-# a.color = :blue
-# a[:color] = :blue
-# a.set :color = :blue
-# a.preferred_color = :blue
-#
-# getters:
-# a.color
-# a[:color]
-# a.get :color
-# a.preferred_color
-#
-Spree::AppConfiguration.class_eval do
- # Preferences related to banner settings
- preference :banner_default_url, :string, :default => '/spree/banners/:id/:style/:basename.:extension'
- preference :banner_path, :string, :default => ':rails_root/public/spree/banners/:id/:style/:basename.:extension'
- preference :banner_url, :string, :default => '/spree/banners/:id/:style/:basename.:extension'
- preference :banner_styles, :string, :default => "{\":mini\":\"48x48>\",\":small\":\"100x100>\",\":large\":\"800x200#\"}"
- preference :banner_default_style, :string, :default => 'small'
-end
@@ -1,7 +1,7 @@
module Spree
class BannerBox < ActiveRecord::Base
- attr_accessible :presentation, :url, :category, :position, :enabled, :attachment
-
+ attr_accessible :alt_text, :url, :category, :position, :enabled, :attachment
+
has_attached_file :attachment,
:url => "/spree/banners/:id/:style_:basename.:extension",
:path => ":rails_root/public/spree/banners/:id/:style_:basename.:extension",
@@ -15,30 +15,36 @@ class BannerBox < ActiveRecord::Base
# save the w,h of the original image (from which others can be calculated)
# we need to look at the write-queue for images which have not been saved yet
after_post_process :find_dimensions
-
+
validates_presence_of :category
validates_attachment_presence :attachment
validates_attachment_content_type :attachment, :content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/jpg', 'image/x-png', 'image/pjpeg'], :message => I18n.t(:images_only)
-
- scope :enable, lambda { |category| {:conditions => {:enabled => true, :category => category}} }
-
+
+ scope :enabled, lambda { |*categories|
+ if categories.empty?
+ where(:enabled => true)
+ else
+ where(:enabled => true).where(:category => categories)
+ end
+ }
+
# Load user defined paperclip settings
include Spree::Core::S3Support
supports_s3 :attachment
-
- Spree::BannerBox.attachment_definitions[:attachment][:styles] = ActiveSupport::JSON.decode(Spree::Config[:banner_styles])
- Spree::BannerBox.attachment_definitions[:attachment][:path] = Spree::Config[:banner_path]
- Spree::BannerBox.attachment_definitions[:attachment][:url] = Spree::Config[:banner_url]
- Spree::BannerBox.attachment_definitions[:attachment][:default_url] = Spree::Config[:banner_default_url]
- Spree::BannerBox.attachment_definitions[:attachment][:default_style] = Spree::Config[:banner_default_style]
-
+
+ Spree::BannerBox.attachment_definitions[:attachment][:styles] = ActiveSupport::JSON.decode(SpreeBanner::Config[:banner_styles]).symbolize_keys!
+ Spree::BannerBox.attachment_definitions[:attachment][:path] = SpreeBanner::Config[:banner_path]
+ Spree::BannerBox.attachment_definitions[:attachment][:url] = SpreeBanner::Config[:banner_url]
+ Spree::BannerBox.attachment_definitions[:attachment][:default_url] = SpreeBanner::Config[:banner_default_url]
+ Spree::BannerBox.attachment_definitions[:attachment][:default_style] = SpreeBanner::Config[:banner_default_style].to_sym
+
def initialize(*args)
super(*args)
last_banner = BannerBox.last
self.position = last_banner ? last_banner.position + 1 : 0
enhance_settings
end
-
+
# for adding banner_boxes which are closely related to existing ones
# define "duplicate_extra" for site-specific actions, eg for additional fields
def duplicate
@@ -54,7 +60,7 @@ def duplicate
p.save!
p
end
-
+
def find_dimensions
temporary = attachment.queued_for_write[:original]
filename = temporary.path unless temporary.nil?
@@ -66,15 +72,19 @@ def find_dimensions
def enhance_settings
extended_hash = {}
- ActiveSupport::JSON.decode(Spree::Config[:banner_styles]).each do |key,value|
+ ActiveSupport::JSON.decode(SpreeBanner::Config[:banner_styles]).each do |key,value|
extended_hash[:"#{key}"] = value
end
Spree::BannerBox.attachment_definitions[:attachment][:styles] = extended_hash
- Spree::BannerBox.attachment_definitions[:attachment][:path] = Spree::Config[:banner_path]
- Spree::BannerBox.attachment_definitions[:attachment][:url] = Spree::Config[:banner_url]
- Spree::BannerBox.attachment_definitions[:attachment][:default_url] = Spree::Config[:banner_default_url]
- Spree::BannerBox.attachment_definitions[:attachment][:default_style] = Spree::Config[:banner_default_style]
+ Spree::BannerBox.attachment_definitions[:attachment][:path] = SpreeBanner::Config[:banner_path]
+ Spree::BannerBox.attachment_definitions[:attachment][:url] = SpreeBanner::Config[:banner_url]
+ Spree::BannerBox.attachment_definitions[:attachment][:default_url] = SpreeBanner::Config[:banner_default_url]
+ Spree::BannerBox.attachment_definitions[:attachment][:default_style] = SpreeBanner::Config[:banner_default_style]
end
-
+
+ def self.categories_for_select
+ unscoped.pluck(:category).uniq.sort
+ end
+
end
end
@@ -0,0 +1,9 @@
+module Spree
+ class BannerConfiguration < Preferences::Configuration
+ preference :banner_default_url, :string, default: '/spree/banners/:id/:style/:basename.:extension'
+ preference :banner_path, :string, default: ':rails_root/public/spree/banners/:id/:style/:basename.:extension'
+ preference :banner_url, :string, default: '/spree/banners/:id/:style/:basename.:extension'
+ preference :banner_styles, :string, default: "{\"mini\":\"48x48>\",\"small\":\"100x100>\",\"large\":\"800x200#\"}"
+ preference :banner_default_style, :string, default: 'small'
+ end
+end
@@ -6,4 +6,4 @@
Deface::Override.new(:virtual_path => "spree/admin/shared/_configuration_menu",
:name => "add_banner_box_settings",
:insert_bottom => "[data-hook='admin_configurations_sidebar_menu'], #admin_configurations_sidebar_menu[data-hook]",
- :text => "<%= configurations_sidebar_menu_item(:banner_box_settings, edit_admin_banner_box_settings_url) %>")
+ :text => "<%= configurations_sidebar_menu_item(Spree.t(:banner_box_settings), edit_admin_banner_box_settings_url) %>")
@@ -18,27 +18,27 @@
<div data-hook="attachment_path" class="field">
<%= label_tag 'preferences[attachment_path]', t(:banner_path) %>
- <%= preference_field_tag 'preferences[banner_path]', Spree::Config[:banner_path], :type => :string %>
+ <%= preference_field_tag 'preferences[banner_path]', SpreeBanner::Config[:banner_path], :type => :string %>
</div>
<div class="alpha eight columns">
<div data-hook="attachment_default_url" class="field">
<%= label_tag 'preferences[attachment_default_url]', t(:banner_default_url) %>
- <%= preference_field_tag 'preferences[banner_default_url]', Spree::Config[:banner_default_url], :type => :string %>
+ <%= preference_field_tag 'preferences[banner_default_url]', SpreeBanner::Config[:banner_default_url], :type => :string %>
</div>
</div>
<div class="alpha eight columns">
<div data-hook="attachment_url" class="field">
<%= label_tag 'preferences[attachment_url]', t(:banner_url) %>
- <%= preference_field_tag 'preferences[banner_url]', Spree::Config[:banner_url], :type => :string %>
+ <%= preference_field_tag 'preferences[banner_url]', SpreeBanner::Config[:banner_url], :type => :string %>
</div>
</div>
<div class="omega four columns">
<div data-hook="attachment_default_style" class="field">
<%= label_tag 'preferences[attachment_default_style]', t(:banner_default_style) %>
- <%= collection_select 'preferences', 'banner_default_style', @styles, :first, :first, {:selected => Spree::Config[:banner_default_style] }, :class => 'select2 fullwidth' %>
+ <%= collection_select 'preferences', 'banner_default_style', @styles, :first, :first, {:selected => SpreeBanner::Config[:banner_default_style] }, :class => 'select2 fullwidth' %>
</div>
</div>
Oops, something went wrong.

0 comments on commit 79b0bcf

Please sign in to comment.