Module: SmartListing::Helper

Defined in:
app/helpers/smart_listing/helper.rb

Defined Under Namespace

Modules: ControllerExtensions Classes: Builder

Instance Method Summary collapse

Instance Method Details

#smart_listing_configObject



191
192
193
# File 'app/helpers/smart_listing/helper.rb', line 191

def smart_listing_config
  SmartListing.config(smart_listing_config_profile)
end

#smart_listing_config_profileObject



187
188
189
# File 'app/helpers/smart_listing/helper.rb', line 187

def smart_listing_config_profile
  defined?(super) ? super : :default
end

#smart_listing_controls_for(name, *args, &block) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/helpers/smart_listing/helper.rb', line 234

def smart_listing_controls_for name, *args, &block
  smart_listing = @smart_listings.try(:[], name)

  classes = [smart_listing_config.classes(:controls), args.first.try(:[], :class)]

  form_tag(smart_listing.try(:href) || {}, :remote => smart_listing.try(:remote?) || true, :method => :get, :class => classes, :data => {smart_listing_config.data_attributes(:main) => name}) do
    concat((:div, :style => "margin:0;padding:0;display:inline") do
      concat(hidden_field_tag("#{smart_listing.try(:base_param)}[_]", 1, :id => nil)) # this forces smart_listing_update to refresh the list
    end)
    concat(capture(&block))
  end
end

#smart_listing_for(name, *args, &block) ⇒ Object

Outputs smart list container

Raises:

  • (ArgumentError)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/helpers/smart_listing/helper.rb', line 196

def smart_listing_for name, *args, &block
  raise ArgumentError, "Missing block" unless block_given?
  name = name.to_sym
  options = args.extract_options!
  bare = options.delete(:bare)

  builder = Builder.new(name, @smart_listings[name], self, options, block)

  output = ""

  data = {}
  data[smart_listing_config.data_attributes(:max_count)] = @smart_listings[name].max_count if @smart_listings[name].max_count && @smart_listings[name].max_count > 0
  data[smart_listing_config.data_attributes(:item_count)] = @smart_listings[name].count
  data[smart_listing_config.data_attributes(:href)] = @smart_listings[name].href if @smart_listings[name].href
  data[smart_listing_config.data_attributes(:callback_href)] = @smart_listings[name].callback_href if @smart_listings[name].callback_href
  data.merge!(options[:data]) if options[:data]

  if bare
    output = capture(builder, &block)
  else
    output = (:div, :class => smart_listing_config.classes(:main), :id => name, :data => data) do
      concat((:div, "", :class => smart_listing_config.classes(:loading)))
      concat((:div, :class => smart_listing_config.classes(:content)) do
        concat(capture(builder, &block))
      end)
    end
  end

  output
end

#smart_listing_item(*args) ⇒ Object

Renders single item (i.e for create, update actions)

Possible calls: smart_listing_item name, item_action, object = nil, partial = nil, options = {} smart_listing_item item_action, object = nil, partial = nil, options = {}



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'app/helpers/smart_listing/helper.rb', line 338

def smart_listing_item *args
  options = args.extract_options!
  if [:create, :create_continue, :destroy, :edit, :new, :remove, :update].include?(args[1])
    name = args[0]
    item_action = args[1]
    object = args[2]
    partial = args[3]
  else
    name = (options[:name] || controller_name).to_sym
    item_action = args[0]
    object = args[1]
    partial = args[2]
  end
  type = object.class.name.downcase.to_sym if object
  id = options[:id] || object.try(:id)
  valid = options[:valid] if options.has_key?(:valid)
  object_key = options.delete(:object_key) || :object
  new = options.delete(:new)

  render(:partial => "smart_listing/item/#{item_action.to_s}", :locals => {:name => name, :id => id, :valid => valid, :object_key => object_key, :object => object, :part => partial, :new => new})
end

#smart_listing_item_actions(actions = []) ⇒ Object

Render item action buttons (ie. edit, destroy and custom ones)



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'app/helpers/smart_listing/helper.rb', line 248

def smart_listing_item_actions actions = []
  (:span) do
    actions.each do |action|
      next unless action.is_a?(Hash)

      locals = {
        :action_if => action.has_key?(:if) ? action[:if] : true,
        :url => action.delete(:url),
        :icon => action.delete(:icon),
        :title => action.delete(:title),
      }

      template = nil
      action_name = action[:name].to_sym

      case action_name
      when :show
        locals[:icon] ||= smart_listing_config.classes(:icon_show)
    template = 'action_show'
      when :edit
        locals[:icon] ||= smart_listing_config.classes(:icon_edit)
    template = 'action_edit'
      when :destroy
        locals[:icon] ||= smart_listing_config.classes(:icon_trash)
        locals.merge!(
          :confirmation => action.delete(:confirmation),
        )
        template = 'action_delete'
      when :custom
        locals.merge!(
          :html_options => action,
        )
        template = 'action_custom'
      end

      locals[:icon] = [locals[:icon], smart_listing_config.classes(:muted)] if !locals[:action_if]

      if template
        concat(render(:partial => "smart_listing/#{template}", :locals => locals))
      else
        concat(render(:partial => "smart_listing/action_#{action_name}", :locals => {:action => action}))
      end
    end
  end
end

#smart_listing_limit_left(name) ⇒ Object



294
295
296
297
298
299
# File 'app/helpers/smart_listing/helper.rb', line 294

def smart_listing_limit_left name
  name = name.to_sym
  smart_listing = @smart_listings[name]

  smart_listing.max_count - smart_listing.count
end

#smart_listing_render(name = controller_name, *args) ⇒ Object



227
228
229
230
231
232
# File 'app/helpers/smart_listing/helper.rb', line 227

def smart_listing_render name = controller_name, *args
  options = args.dup.extract_options!
  smart_listing_for(name, *args) do |smart_listing|
    concat(smart_listing.render_list(options[:locals]))
  end
end

#smart_listing_update(*args) ⇒ Object

Updates smart listing

Posible calls: smart_listing_update name, options = {} smart_listing_update options = {}



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'app/helpers/smart_listing/helper.rb', line 309

def smart_listing_update *args
  options = args.extract_options!
  name = (args[0] || options[:name] || controller_name).to_sym
  smart_listing = @smart_listings[name]

  # don't update list if params are missing (prevents interfering with other lists)
  if params.keys.select{|k| k.include?("smart_listing")}.present? && !params[smart_listing.base_param]
    return unless options[:force]
  end

  builder = Builder.new(name, smart_listing, self, {}, nil)
  render(:partial => 'smart_listing/update_list', :locals => {
    :name => smart_listing.name,
    :part => smart_listing.partial,
    :smart_listing => builder,
    :smart_listing_data => {
      smart_listing_config.data_attributes(:params) => smart_listing.all_params,
      smart_listing_config.data_attributes(:max_count) => smart_listing.max_count,
      smart_listing_config.data_attributes(:item_count) => smart_listing.count,
    },
    :locals => options[:locals] || {}
  })
end