Class: Listings::Base

Inherits:
Object
  • Object
show all
Includes:
ERB::Util, ConfigurationMethods, ViewHelperMethods
Defined in:
lib/listings/base.rb,
lib/listings/filters/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



22
23
24
# File 'lib/listings/base.rb', line 22

def initialize
  @page_size = self.class.page_size
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



214
215
216
# File 'lib/listings/base.rb', line 214

def method_missing(m, *args, &block)
  view_context.send(m, *args, &block)
end

Instance Attribute Details

#data_sourceObject (readonly)

Returns the value of attribute data_source.



19
20
21
# File 'lib/listings/base.rb', line 19

def data_source
  @data_source
end

#paramsObject

Returns the value of attribute params.



14
15
16
# File 'lib/listings/base.rb', line 14

def params
  @params
end

#search_criteriaObject

Returns the value of attribute search_criteria.



16
17
18
# File 'lib/listings/base.rb', line 16

def search_criteria
  @search_criteria
end

#search_filtersObject

Returns the value of attribute search_filters.



17
18
19
# File 'lib/listings/base.rb', line 17

def search_filters
  @search_filters
end

#view_contextObject

html_escape methods are private in the view_context so they need to be included in order to be available



13
14
15
# File 'lib/listings/base.rb', line 13

def view_context
  @view_context
end

Instance Method Details

#client_configObject



239
240
241
# File 'lib/listings/base.rb', line 239

def client_config
  { push_url: Listings.configuration.push_url }
end

#collect_filter(text, filter, filters_hash) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/listings/base.rb', line 56

def collect_filter(text, filter, filters_hash)
  ["#{filter}:\s*\"([^\"]+)\"",
   "#{filter}:\s*\'([^\']+)\'",
   "#{filter}:\s*(\\S+)"].each do |pattern|
    m = Regexp.new(pattern, Regexp::IGNORECASE).match(text)
    if m
      filters_hash[filter] = m[1]
      return "#{m.pre_match.strip} #{m.post_match.strip}".strip
    end
  end

  return text
end

#column_with_key(key) ⇒ Object



144
145
146
# File 'lib/listings/base.rb', line 144

def column_with_key(key)
  self.columns.find { |c| c.key == key }
end

#export_eachObject



180
181
182
183
184
# File 'lib/listings/base.rb', line 180

def export_each
  self.items.each do |item|
    yield item
  end
end

#export_filename(format) ⇒ Object



231
232
233
# File 'lib/listings/base.rb', line 231

def export_filename(format)
  "#{kind.gsub(' ', '_')}_#{Time.now.to_s.gsub(' ', '_')}.#{format}"
end

#filter_items(params) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/listings/base.rb', line 70

def filter_items(params)
  columns # prepare columns
  filters # prepare filters

  self.page = params[param_page] || 1
  self.scope = scope_by_name(params[param_scope])
  self.search = params[param_search]
  parse_search

  unless scope.nil?
    data_source.scope do |items|
      scope.apply(self, items)
    end
  end

  if search_criteria.present? && self.searchable?
    search_fields = self.columns.select(&:searchable?).map &:field
    data_source.search(search_fields, search_criteria)
  end

  if filterable?
    filters_to_render.each do |filter_view|
      filter_view.prepare_values
    end

    self.search_filters.each do |key, filter_value|
      filter_with_key(key).apply_filter(filter_value)
    end
  end

  if params.include?(param_sort_by)
    sort_col = column_with_key(params[param_sort_by])
    sort_col.sort = params[param_sort_direction]
    data_source.sort(sort_col.field, params[param_sort_direction])
  end

  if paginated?
    data_source.paginate(page, page_size)
  end

  self.items
end

#filter_with_key(key) ⇒ Object



148
149
150
# File 'lib/listings/base.rb', line 148

def filter_with_key(key)
  self.filters.find { |c| c.key == key }
end

#filterable?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/listings/base.rb', line 160

def filterable?
  !self.filters.empty?
end

#filters_to_renderObject



235
236
237
# File 'lib/listings/base.rb', line 235

def filters_to_render
  filters.select { |f| f.render? }
end

#formatObject



176
177
178
# File 'lib/listings/base.rb', line 176

def format
  (params[:format] || :html).to_sym
end

#human_name(field) ⇒ Object



152
153
154
# File 'lib/listings/base.rb', line 152

def human_name(field)
  field.human_name
end

#kaminari_themeObject



218
219
220
221
222
223
224
225
# File 'lib/listings/base.rb', line 218

def kaminari_theme
  case theme
  when 'twitter-bootstrap-2'
    'twitter-bootstrap'
  else
    theme
  end
end

#nameObject



26
27
28
# File 'lib/listings/base.rb', line 26

def name
  self.class.name.underscore.sub(/_listing$/,'')
end

#paginated?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/listings/base.rb', line 126

def paginated?
  self.page_size != :none
end

#param_pageObject



31
# File 'lib/listings/base.rb', line 31

def param_page; :page; end

#param_scopeObject



30
# File 'lib/listings/base.rb', line 30

def param_scope; :scope; end

#param_searchObject



32
# File 'lib/listings/base.rb', line 32

def param_search; :s; end

#param_sort_byObject



33
# File 'lib/listings/base.rb', line 33

def param_sort_by; :sort_by; end

#param_sort_directionObject



34
# File 'lib/listings/base.rb', line 34

def param_sort_direction; :sort_d; end

#parse_filter(text, filter_keys) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/listings/base.rb', line 47

def parse_filter(text, filter_keys)
  filters = {}
  filter_keys.sort_by {|k| -k.length }.each do |key|
    text = collect_filter text, key, filters
  end

  return filters, text
end

#parse_searchObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/listings/base.rb', line 36

def parse_search
  if !filterable?
    # if it is not filterable, all the search is used as search criteria
    self.search_criteria = self.search
    self.search_filters = {}
  else
    # otherwise parse the search stripping out allowed filterable fields
    self.search_filters, self.search_criteria = parse_filter(self.search, self.filters.map(&:key))
  end
end

#query_items(params) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/listings/base.rb', line 113

def query_items(params)
  @params = params

  @data_source = self.model_class
  @data_source = Sources::DataSource.for(@data_source) unless @data_source.is_a?(Sources::DataSource)

  filter_items(self.scoped_params)
end

#render_filters_at?(loc) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/listings/filters/base.rb', line 3

def render_filters_at?(loc)
  self.filterable? && layout_options[:filters] == loc
end

#scope_by_name(name) ⇒ Object



134
135
136
137
138
# File 'lib/listings/base.rb', line 134

def scope_by_name(name)
  default_scope = scopes.find { |s| s.is_default? }
  selected_scope = scopes.find { |s| s.name == name.try(:to_sym) }
  selected_scope || default_scope
end

#scoped_paramsObject



130
131
132
# File 'lib/listings/base.rb', line 130

def scoped_params
  @params.except(:listing, :controller, :action)
end

#search_dataObject



172
173
174
# File 'lib/listings/base.rb', line 172

def search_data
  { criteria: self.search_criteria, filters: self.search_filters }
end

#searchable?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/listings/base.rb', line 156

def searchable?
  self.columns.any? &:searchable?
end

#selectable?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/listings/base.rb', line 122

def selectable?
  self.class.selectable?
end

#selectable_id(model) ⇒ Object



164
165
166
# File 'lib/listings/base.rb', line 164

def selectable_id(model)
  model.id
end

#send_csv(controller) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/listings/base.rb', line 186

def send_csv(controller)
  csv_filename = self.export_filename(:csv)

  controller.headers["X-Accel-Buffering"] = "no"
  controller.headers["Cache-Control"] = "no-cache"
  controller.headers["Content-Type"] = "text/csv; charset=utf-8"
  controller.headers["Content-Disposition"] = %(attachment; filename="#{csv_filename}")
  controller.headers["Transfer-Encoding"] = "chunked"
  controller.headers.delete("Content-Length")

  controller.response_body = Enumerator.new do |lines|
    lines << self.columns.map { |c| c.human_name }.to_csv

    self.export_each do |item|
      row = []
      self.columns.each do |col|
        row << col.value_for(item)
      end
      lines << row.to_csv
    end
  end
end

#send_xls(controller) ⇒ Object



209
210
211
212
# File 'lib/listings/base.rb', line 209

def send_xls(controller)
  controller.headers["Content-Disposition"] = %(attachment; filename="#{self.export_filename(:xls)}")
  controller.render 'listings/export'
end

#themeObject



227
228
229
# File 'lib/listings/base.rb', line 227

def theme
  Listings.configuration.theme
end

#urlObject



168
169
170
# File 'lib/listings/base.rb', line 168

def url
  view_context.listings.listing_full_path(self.name, self.params)
end

#value_for(column, item) ⇒ Object



140
141
142
# File 'lib/listings/base.rb', line 140

def value_for(column, item)
  column.value_for(self, item)
end