Class: Listings::Base
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Base
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_source ⇒ Object
Returns the value of attribute data_source.
19
20
21
|
# File 'lib/listings/base.rb', line 19
def data_source
@data_source
end
|
#params ⇒ Object
Returns the value of attribute params.
14
15
16
|
# File 'lib/listings/base.rb', line 14
def params
@params
end
|
#search_criteria ⇒ Object
Returns the value of attribute search_criteria.
16
17
18
|
# File 'lib/listings/base.rb', line 16
def search_criteria
@search_criteria
end
|
#search_filters ⇒ Object
Returns the value of attribute search_filters.
17
18
19
|
# File 'lib/listings/base.rb', line 17
def search_filters
@search_filters
end
|
#view_context ⇒ Object
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_config ⇒ Object
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_each ⇒ Object
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 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
160
161
162
|
# File 'lib/listings/base.rb', line 160
def filterable?
!self.filters.empty?
end
|
#filters_to_render ⇒ Object
235
236
237
|
# File 'lib/listings/base.rb', line 235
def filters_to_render
filters.select { |f| f.render? }
end
|
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_theme ⇒ Object
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
|
#name ⇒ Object
26
27
28
|
# File 'lib/listings/base.rb', line 26
def name
self.class.name.underscore.sub(/_listing$/,'')
end
|
#paginated? ⇒ Boolean
126
127
128
|
# File 'lib/listings/base.rb', line 126
def paginated?
self.page_size != :none
end
|
#param_page ⇒ Object
31
|
# File 'lib/listings/base.rb', line 31
def param_page; :page; end
|
#param_scope ⇒ Object
30
|
# File 'lib/listings/base.rb', line 30
def param_scope; :scope; end
|
#param_search ⇒ Object
32
|
# File 'lib/listings/base.rb', line 32
def param_search; :s; end
|
#param_sort_by ⇒ Object
33
|
# File 'lib/listings/base.rb', line 33
def param_sort_by; :sort_by; end
|
#param_sort_direction ⇒ Object
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_search ⇒ Object
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/listings/base.rb', line 36
def parse_search
if !filterable?
self.search_criteria = self.search
self.search_filters = {}
else
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
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_params ⇒ Object
130
131
132
|
# File 'lib/listings/base.rb', line 130
def scoped_params
@params.except(:listing, :controller, :action)
end
|
#search_data ⇒ Object
172
173
174
|
# File 'lib/listings/base.rb', line 172
def search_data
{ criteria: self.search_criteria, filters: self.search_filters }
end
|
#searchable? ⇒ Boolean
156
157
158
|
# File 'lib/listings/base.rb', line 156
def searchable?
self.columns.any? &:searchable?
end
|
#selectable? ⇒ 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.["X-Accel-Buffering"] = "no"
controller.["Cache-Control"] = "no-cache"
controller.["Content-Type"] = "text/csv; charset=utf-8"
controller.["Content-Disposition"] = %(attachment; filename="#{csv_filename}")
controller.["Transfer-Encoding"] = "chunked"
controller..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.["Content-Disposition"] = %(attachment; filename="#{self.export_filename(:xls)}")
controller.render 'listings/export'
end
|
#url ⇒ Object
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
|