Class: SmartListing::Helper::Builder

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(smart_listing_name, smart_listing, template, options, proc) ⇒ Builder

Returns a new instance of Builder.



42
43
44
# File 'app/helpers/smart_listing/helper.rb', line 42

def initialize(smart_listing_name, smart_listing, template, options, proc)
  @smart_listing_name, @smart_listing, @template, @options, @proc = smart_listing_name, smart_listing, template, options, proc
end

Instance Method Details

#collectionObject



56
57
58
# File 'app/helpers/smart_listing/helper.rb', line 56

def collection
  @smart_listing.collection
end

#countObject



172
173
174
# File 'app/helpers/smart_listing/helper.rb', line 172

def count
  @smart_listing.count
end

#empty?Boolean

Check if smart list is empty

Returns:

  • (Boolean)


61
62
63
# File 'app/helpers/smart_listing/helper.rb', line 61

def empty?
  @smart_listing.count == 0
end

#item_new(options = {}, &block) ⇒ Object

Add new item button & placeholder to list



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/helpers/smart_listing/helper.rb', line 139

def item_new options = {}, &block
  no_records_classes = [@template.smart_listing_config.classes(:no_records)]
  no_records_classes << @template.smart_listing_config.classes(:hidden) unless empty?
  new_item_button_classes = []
  new_item_button_classes << @template.smart_listing_config.classes(:hidden) if max_count?

  locals = {
    :colspan => options.delete(:colspan),
    :no_items_classes => no_records_classes,
    :no_items_text => options.delete(:no_items_text) || @template.t("smart_listing.msgs.no_items"),
    :new_item_button_url => options.delete(:link),
    :new_item_button_classes => new_item_button_classes,
    :new_item_button_text => options.delete(:text) || @template.t("smart_listing.actions.new"),
    :new_item_autoshow => block_given?,
    :new_item_content => nil,
  }

  unless block_given?
    locals[:placeholder_classes] = [@template.smart_listing_config.classes(:new_item_placeholder), @template.smart_listing_config.classes(:hidden)]
    locals[:new_item_action_classes] = [@template.smart_listing_config.classes(:new_item_action)]
    locals[:new_item_action_classes] << @template.smart_listing_config.classes(:hidden) if !empty? && max_count?

    @template.render(:partial => 'smart_listing/item_new', :locals => default_locals.merge(locals))
  else
    locals[:placeholder_classes] = [@template.smart_listing_config.classes(:new_item_placeholder)]
    locals[:placeholder_classes] << @template.smart_listing_config.classes(:hidden) if !empty? && max_count?
    locals[:new_item_action_classes] = [@template.smart_listing_config.classes(:new_item_action), @template.smart_listing_config.classes(:hidden)]

    locals[:new_item_content] = @template.capture(&block)
    @template.render(:partial => 'smart_listing/item_new', :locals => default_locals.merge(locals))
  end
end

#max_count?Boolean

Check if smart list reached its item max count

Returns:

  • (Boolean)


177
178
179
180
# File 'app/helpers/smart_listing/helper.rb', line 177

def max_count?
  return false if @smart_listing.max_count.nil?
  @smart_listing.count >= @smart_listing.max_count
end

#nameObject



46
47
48
# File 'app/helpers/smart_listing/helper.rb', line 46

def name
  @smart_listing_name
end

#paginate(options = {}) ⇒ Object



50
51
52
53
54
# File 'app/helpers/smart_listing/helper.rb', line 50

def paginate options = {}
  if @smart_listing.collection.respond_to? :current_page
    @template.paginate @smart_listing.collection, {:remote => @smart_listing.remote?, :param_name => @smart_listing.param_name(:page), :params => UNSAFE_PARAMS}.merge(@smart_listing.kaminari_options)
  end
end


80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/smart_listing/helper.rb', line 80

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.all_params(:per_page => page, :page => 1))))
  end

  locals = {
    :page => page,
    :url => url,
  }

  @template.render(:partial => 'smart_listing/pagination_per_page_link', :locals => default_locals.merge(locals))
end


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/smart_listing/helper.rb', line 65

def pagination_per_page_links options = {}
  container_classes = [@template.smart_listing_config.classes(:pagination_per_page)]
  container_classes << @template.smart_listing_config.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



127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/smart_listing/helper.rb', line 127

def render options = {}, locals = {}, &block
  if locals.empty?
    options[:locals] ||= {}
    options[:locals].merge!(:smart_listing => self)
  else
    locals.merge!({:smart_listing => self})
  end

  @template.render options, locals, &block
end

#render_list(locals = {}) ⇒ Object

Renders the main partial (whole list)



120
121
122
123
124
# File 'app/helpers/smart_listing/helper.rb', line 120

def render_list locals = {}
  if @smart_listing.partial
    @template.render :partial => @smart_listing.partial, :locals => {:smart_listing => self}.merge(locals || {})
  end
end

#sortable(title, attribute, options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/smart_listing/helper.rb', line 93

def sortable title, attribute, options = {}
  dirs = options[:sort_dirs] || @smart_listing.sort_dirs || [nil, "asc", "desc"]

  next_index = dirs.index(@smart_listing.sort_order(attribute)).nil? ? 0 : (dirs.index(@smart_listing.sort_order(attribute)) + 1) % dirs.length

  sort_params = {
    attribute => dirs[next_index]
  }

  locals = {
    :order => @smart_listing.sort_order(attribute),
    :url => @template.url_for(sanitize_params(@template.params.merge(@smart_listing.all_params(:sort => sort_params)))),
    :container_classes => [@template.smart_listing_config.classes(:sortable)],
    :attribute => attribute,
    :title => title
  }

  @template.render(:partial => 'smart_listing/sortable', :locals => default_locals.merge(locals))
end

#update(options = {}) ⇒ Object



113
114
115
116
117
# File 'app/helpers/smart_listing/helper.rb', line 113

def update options = {}
  part = options.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