Class: SmartListing::Helper::Builder
- Inherits:
-
Object
- Object
- SmartListing::Helper::Builder
- Defined in:
- app/helpers/smart_listing/helper.rb
Constant Summary collapse
- UNSAFE_PARAMS =
Params that should not be visible in pagination links (pages, per-page, sorting, etc.)
{:authenticity_token => nil, :utf8 => nil}
Instance Method Summary collapse
- #collection ⇒ Object
-
#empty? ⇒ Boolean
Check if smart list is empty.
-
#initialize(smart_listing_name, smart_listing, template, options, proc) ⇒ Builder
constructor
A new instance of Builder.
-
#item_new(options = {}) ⇒ Object
Add new item button & placeholder to list.
-
#max_count? ⇒ Boolean
Check if smart list reached its item max count.
- #paginate(options = {}) ⇒ Object
- #pagination_per_page_link(page) ⇒ Object
- #pagination_per_page_links(options = {}) ⇒ Object
-
#render(options = {}, locals = {}, &block) ⇒ Object
Basic render block wrapper that adds smart_listing reference to local variables.
-
#render_list ⇒ Object
Renders the main partial (whole list).
- #sortable(title, attribute, options = {}) ⇒ Object
- #update(options = {}) ⇒ Object
Constructor Details
#initialize(smart_listing_name, smart_listing, template, options, proc) ⇒ Builder
Returns a new instance of Builder.
27 28 29 |
# File 'app/helpers/smart_listing/helper.rb', line 27 def initialize(smart_listing_name, smart_listing, template, , proc) @smart_listing_name, @smart_listing, @template, , @proc = smart_listing_name, smart_listing, template, , proc end |
Instance Method Details
#collection ⇒ Object
37 38 39 |
# File 'app/helpers/smart_listing/helper.rb', line 37 def collection @smart_listing.collection end |
#empty? ⇒ Boolean
Check if smart list is empty
138 139 140 |
# File 'app/helpers/smart_listing/helper.rb', line 138 def empty? @smart_listing.count == 0 end |
#item_new(options = {}) ⇒ Object
Add new item button & placeholder to list
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/helpers/smart_listing/helper.rb', line 115 def item_new = {} new_item_action_classes = %w{new_item_action} new_item_action_classes << "hidden" if !empty? && max_count? no_records_classes = %w{no_records} no_records_classes << "hidden" unless empty? = [] << "hidden" if max_count? locals = { :placeholder_classes => %w{new_item_placeholder hidden}, :new_item_action_classes => new_item_action_classes, :colspan => .delete(:colspan), :no_items_classes => no_records_classes, :no_items_text => .delete(:no_items_text), :new_item_button_url => .delete(:link), :new_item_button_classes => , :new_item_button_text => .delete(:text), } @template.render(:partial => 'smart_listing/item_new', :locals => default_locals.merge(locals)) end |
#max_count? ⇒ Boolean
Check if smart list reached its item max count
143 144 145 146 |
# File 'app/helpers/smart_listing/helper.rb', line 143 def max_count? return false if @smart_listing.max_count.nil? @smart_listing.count >= @smart_listing.max_count end |
#paginate(options = {}) ⇒ Object
31 32 33 34 35 |
# File 'app/helpers/smart_listing/helper.rb', line 31 def paginate = {} if @smart_listing.collection.respond_to? :current_page @template.paginate @smart_listing.collection, {:remote => true, :param_name => @smart_listing.param_names[:page], :params => UNSAFE_PARAMS}.merge(@smart_listing.) end end |
#pagination_per_page_link(page) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/helpers/smart_listing/helper.rb', line 56 def pagination_per_page_link page if @smart_listing.per_page.to_i != page url = @template.url_for(sanitize_params(@template.params.merge(@smart_listing.param_names[:per_page] => page, @smart_listing.param_names[:page] => 1))) end locals = { :page => page, :url => url, } @template.render(:partial => 'smart_listing/pagination_per_page_link', :locals => default_locals.merge(locals)) end |
#pagination_per_page_links(options = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/helpers/smart_listing/helper.rb', line 41 def pagination_per_page_links = {} container_classes = ["pagination_per_page"] container_classes << "hidden" if empty? per_page_sizes = @smart_listing.page_sizes.clone per_page_sizes.push(0) if @smart_listing.unlimited_per_page? locals = { :container_classes => container_classes, :per_page_sizes => per_page_sizes, } @template.render(:partial => 'smart_listing/pagination_per_page_links', :locals => default_locals.merge(locals)) end |
#render(options = {}, locals = {}, &block) ⇒ Object
Basic render block wrapper that adds smart_listing reference to local variables
103 104 105 106 107 108 109 110 111 112 |
# File 'app/helpers/smart_listing/helper.rb', line 103 def render = {}, locals = {}, &block if locals.empty? [:locals] ||= {} [:locals].merge!(:smart_listing => self) else locals.merge!({:smart_listing => self}) end @template.render , locals, &block end |
#render_list ⇒ Object
Renders the main partial (whole list)
96 97 98 99 100 |
# File 'app/helpers/smart_listing/helper.rb', line 96 def render_list if @smart_listing.partial @template.render :partial => @smart_listing.partial, :locals => {:smart_listing => self} end end |
#sortable(title, attribute, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/helpers/smart_listing/helper.rb', line 69 def sortable title, attribute, = {} extra = .delete(:extra) sort_params = { @smart_listing.param_names[:sort_attr] => attribute, @smart_listing.param_names[:sort_order] => (@smart_listing.sort_order == "asc") ? "desc" : "asc", @smart_listing.param_names[:sort_extra] => extra } locals = { :ordered => @smart_listing.sort_attr == attribute && (!@smart_listing.sort_extra || @smart_listing.sort_extra == extra.to_s), :url => @template.url_for(sanitize_params(@template.params.merge(sort_params))), :container_classes => ["sortable"], :attribute => attribute, :title => title } @template.render(:partial => 'smart_listing/sortable', :locals => default_locals.merge(locals)) end |
#update(options = {}) ⇒ Object
89 90 91 92 93 |
# File 'app/helpers/smart_listing/helper.rb', line 89 def update = {} part = .delete(:partial) || @smart_listing.partial || @smart_listing_name @template.render(:partial => 'smart_listing/update_list', :locals => {:name => @smart_listing_name, :part => part, :smart_listing => self}) end |