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.



27
28
29
# File 'app/helpers/smart_listing/helper.rb', line 27

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



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

Returns:

  • (Boolean)


125
126
127
# File 'app/helpers/smart_listing/helper.rb', line 125

def empty?
  @smart_listing.count == 0
end

#item_new(options = {}) ⇒ Object

Add new item button & placeholder to list



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/helpers/smart_listing/helper.rb', line 107

def item_new options = {}
  @template.concat(@template.(:tr, '', :class => "info new_item_placeholder disabled"))
  @template.concat(@template.(:tr, :class => "info new_item_action #{'disabled' if !empty? && max_count?}") do
    @template.concat(@template.(:td, :colspan => options.delete(:colspan)) do
      @template.concat(@template.(:p, :class => "no_records pull-left #{'disabled' unless empty?}") do
        @template.concat(options.delete(:no_items_text))
      end)
      @template.concat(@template.link_to(options.delete(:link), :remote => true, :class => "btn pull-right #{'disabled' if max_count?}") do
        @template.concat(@template.(:i, '', :class => "glyphicon glyphicon-plus"))
        @template.concat(" ")
        @template.concat(options.delete(:text))
      end)
    end)
  end)
  nil
end

#max_count?Boolean

Check if smart list reached its item max count

Returns:

  • (Boolean)


130
131
132
133
# File 'app/helpers/smart_listing/helper.rb', line 130

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 options = {}
  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
  end
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/helpers/smart_listing/helper.rb', line 41

def pagination_per_page_links options = {}
  @template.(:div, :class => "pagination_per_page #{'disabled' if empty?}") do
    if @smart_listing.count > @smart_listing.page_sizes.first
      @template.concat(@template.t('views.pagination.per_page'))
      per_page_sizes = @smart_listing.page_sizes.clone
      per_page_sizes.push(0) if @smart_listing.unlimited_per_page?
      per_page_sizes.each do |p|
        name = p == 0 ? @template.t('views.pagination.unlimited') : p
        if @smart_listing.per_page.to_i != p
          @template.concat(@template.link_to(name, @template.url_for(sanitize_params(@template.params.merge(@smart_listing.param_names[:per_page] => p, @smart_listing.param_names[:page] => 1))), :remote => true))
        else 
          @template.concat(@template.(:span, name))
        end
        break if p > @smart_listing.count
      end
      @template.concat ' | '
    end if @smart_listing.options[:paginate]
    @template.concat(@template.t('views.pagination.total'))
    @template.concat(@template.(:span, @smart_listing.count, :class => "count"))
  end
end

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

Basic render block wrapper that adds smart_listing reference to local variables



96
97
98
99
100
101
102
103
104
# File 'app/helpers/smart_listing/helper.rb', line 96

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_listObject

Renders the main partial (whole list)



89
90
91
92
93
# File 'app/helpers/smart_listing/helper.rb', line 89

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

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



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

def sortable title, attribute, options = {}
  extra = options.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
  }

  @template.link_to(@template.url_for(sanitize_params(@template.params.merge(sort_params))), :class => "sortable", :data => {:attr => attribute}, :remote => true) do
    @template.concat(title)
    if @smart_listing.sort_attr == attribute && (!@smart_listing.sort_extra || @smart_listing.sort_extra == extra.to_s)
      @template.concat(@template.(:span, "", :class => (@smart_listing.sort_order == "asc" ? "glyphicon glyphicon-chevron-up" : "glyphicon glyphicon-chevron-down"))) 
    else
      @template.concat(@template.(:span, "", :class => "glyphicon glyphicon-resize-vertical"))
    end
  end
end

#update(options = {}) ⇒ Object



82
83
84
85
86
# File 'app/helpers/smart_listing/helper.rb', line 82

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