cowtech-rails 2.8.1.0 → 2.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,102 +5,102 @@
5
5
  #
6
6
 
7
7
  module Cowtech
8
- module RubyOnRails
9
- module Helpers
10
- module ApplicationHelper
11
- def application_info
12
- @application_info = YAML.load_file(Rails.root + "config/application_info.yml") if !@application_info
13
- @application_info
14
- end
15
-
16
- def full_controller_name(css = false)
17
- rv = self.controller_path
18
- !css ? rv : rv.gsub("/", "_")
19
- end
20
-
21
- def location_name(action = nil, controller = nil)
22
- controller = self.full_controller_name if !controller
23
- action = self.action_name if !action
24
- "#{controller}##{action}"
25
- end
26
-
27
- def additional_tag(what = :js, *args)
28
- if what == :js then
29
- args.insert(0, "app/#{self.full_controller_name}.js")
30
- javascript_include_tag(*args)
31
- elsif what == :css then
32
- args.insert(0, "app/#{self.full_controller_name}.css")
33
- stylesheet_link_tag(*args)
34
- end
35
- end
36
-
37
- def google_font_stylesheet(font, additional = nil, version = "1")
38
- stylesheet_link_tag("http://fonts.googleapis.com/css?family=#{CGI.escape(font)}#{additional}&v=#{version}", :media => :all)
39
- end
40
-
41
- def get_data(key = nil, default = "")
42
- @controller_data.is_a?(Hash) ? @controller_data.fetch(key.to_sym, default) : default
43
- end
44
-
45
- def set_data(key, value)
46
- @controller_data = {} if !(defined?(@controller_data) && @controller_data.is_a?(Hash))
47
- @controller_data[key.to_sym] = value
48
- end
49
-
50
- def get_param(key, default = nil)
51
- params[key].blank? ? default : params[key]
52
- end
53
-
54
- def _normalize_type(format = nil)
55
- format = :text if !([:text, :json, :jsonp].include?(format))
56
- format
57
- end
58
-
59
- def setup_json_response(type = :base)
60
- ApplicationController.setup_json_response(type)
61
- end
62
-
63
- def custom_respond_with(data, format = nil, status = :ok)
64
- return if performed?
65
-
66
- format = request.format if format.blank?
67
- format = self._normalize_type(format)
68
-
69
- if format == :text then
70
- render :text => data, :status => status
71
- elsif format == :json then
72
- render :json => data, :status => status
73
- elsif format == :jsonp then
74
- render :json => data, :status => status, :callback => params[:callback]
75
- end
76
- end
77
-
78
- def debug(what, type = :yaml)
79
- msg = ""
80
-
81
- if type == :json then
82
- begin
83
- msg = JSON.pretty_generate(what)
84
- rescue Exception => e
85
- msg = what.to_json
86
- end
87
- elsif type == :yaml then
88
- msg = what.to_yaml
89
- else
90
- msg = what.inspect
91
- end
92
-
93
- rv = ""
94
- case type.to_sym
95
- when :json
96
- rv = render_to_string(:json => msg)
97
- else
98
- rv = render_to_string(:text => msg)
99
- end
100
-
101
- self.response_body = rv
102
- end
103
- end
104
- end
105
- end
8
+ module RubyOnRails
9
+ module Helpers
10
+ module ApplicationHelper
11
+ def application_info
12
+ @application_info = YAML.load_file(Rails.root + "config/application_info.yml") if !@application_info
13
+ @application_info
14
+ end
15
+
16
+ def full_controller_name(css = false)
17
+ rv = self.controller_path
18
+ !css ? rv : rv.gsub("/", "_")
19
+ end
20
+
21
+ def location_name(action = nil, controller = nil)
22
+ controller = self.full_controller_name if !controller
23
+ action = self.action_name if !action
24
+ "#{controller}##{action}"
25
+ end
26
+
27
+ def additional_tag(what = :js, *args)
28
+ if what == :js then
29
+ args.insert(0, "app/#{self.full_controller_name}.js")
30
+ javascript_include_tag(*args)
31
+ elsif what == :css then
32
+ args.insert(0, "app/#{self.full_controller_name}.css")
33
+ stylesheet_link_tag(*args)
34
+ end
35
+ end
36
+
37
+ def google_font_stylesheet(font, additional = nil, version = "1")
38
+ stylesheet_link_tag("http://fonts.googleapis.com/css?family=#{CGI.escape(font)}#{additional}&v=#{version}", :media => :all)
39
+ end
40
+
41
+ def get_data(key = nil, default = "")
42
+ @controller_data.is_a?(Hash) ? @controller_data.fetch(key.to_sym, default) : default
43
+ end
44
+
45
+ def set_data(key, value)
46
+ @controller_data = {} if !(defined?(@controller_data) && @controller_data.is_a?(Hash))
47
+ @controller_data[key.to_sym] = value
48
+ end
49
+
50
+ def get_param(key, default = nil)
51
+ params[key].blank? ? default : params[key]
52
+ end
53
+
54
+ def _normalize_type(format = nil)
55
+ format = :text if !([:text, :json, :jsonp].include?(format))
56
+ format
57
+ end
58
+
59
+ def setup_json_response(type = :base)
60
+ ApplicationController.setup_json_response(type)
61
+ end
62
+
63
+ def custom_respond_with(data, format = nil, status = :ok)
64
+ return if performed?
65
+
66
+ format = request.format if format.blank?
67
+ format = self._normalize_type(format)
68
+
69
+ if format == :text then
70
+ render :text => data, :status => status
71
+ elsif format == :json then
72
+ render :json => data, :status => status
73
+ elsif format == :jsonp then
74
+ render :json => data, :status => status, :callback => params[:callback]
75
+ end
76
+ end
77
+
78
+ def debug(what, type = :yaml)
79
+ msg = ""
80
+
81
+ if type == :json then
82
+ begin
83
+ msg = JSON.pretty_generate(what)
84
+ rescue Exception => e
85
+ msg = what.to_json
86
+ end
87
+ elsif type == :yaml then
88
+ msg = what.to_yaml
89
+ else
90
+ msg = what.inspect
91
+ end
92
+
93
+ rv = ""
94
+ case type.to_sym
95
+ when :json
96
+ rv = render_to_string(:json => msg)
97
+ else
98
+ rv = render_to_string(:text => msg)
99
+ end
100
+
101
+ self.response_body = rv
102
+ end
103
+ end
104
+ end
105
+ end
106
106
  end
@@ -5,297 +5,297 @@
5
5
  #
6
6
 
7
7
  module Cowtech
8
- module RubyOnRails
9
- module Helpers
10
- module ArCrudHelper
11
- attr_reader :data_bounds
12
- attr_reader :record
13
-
14
- def crud_get_data
15
- @crud_data ||= {}
16
- end
17
-
18
- def crud_get_class(data = nil)
19
- data = self.crud_get_data if !data
20
- data[:class].constantize
21
- end
22
-
23
- def crud_get_records(data)
24
- data = self.crud_get_data if !data
25
- data[:records]
26
- end
27
-
28
- def crud_query_get_params(data = nil)
29
- data = self.crud_get_data if !data
30
- data[:query_params]
31
- end
32
-
33
- def crud_get_sort_order(data = nil)
34
- data = self.crud_get_data if !data
35
- data[:sort_order]
36
- end
37
-
38
- def crud_has_data?(data = nil)
39
- data = self.crud_get_data if !data
40
- data[:data_bounds].total > 0
41
- end
42
-
43
- def crud_get_pager_data(data = nil)
44
- data = self.crud_get_data if !data
45
- data[:pager_data]
46
- end
47
-
48
- def crud_get_data_bounds(data = nil)
49
- data = self.crud_get_data if !data
50
- data[:data_bounds]
51
- end
52
-
53
- def crud_set_data(data)
54
- @crud_data = data
55
- end
56
-
57
- def crud_set_class(data, table = "")
58
- data = self.crud_get_data if !data
59
- data[:class] = table
60
- end
61
-
62
- def crud_set_records(data, records = nil)
63
- data = self.crud_get_data if !data
64
- data[:records] = records
65
- end
66
-
67
- def crud_finalize_read(data = nil, records = nil, parameter = :count, per_page = nil)
68
- data = self.crud_get_data if !data
69
- records = self.crud_get_records(data) if !records
70
- self.crud_calculate_data_bounds(data, records, per_page || params[parameter])
71
- data[:pager_data] = records.paginate(:page => data[:data_bounds].page, :per_page => data[:data_bounds].per_page, :total_entries => data[:data_bounds].total) if records.respond_to?(:paginate)
72
- end
73
-
74
- def crud_query_initialize(data = nil, force = false)
75
- data = self.crud_get_data if !data
76
- data[:query_expr] = [] if !data[:query_expr] || force
77
- data[:query_params] = {} if !data[:query_params] || force
78
- data[:query_initialized] = true
79
- end
80
-
81
- def crud_query_get(data = nil, query = nil)
82
- data = self.crud_get_data if !data
83
- self.crud_query_initialize(data) if !data[:query_initialized]
84
- query = data[:query_expr] if !query
85
-
86
- query.count.times { |i| query[i] = "(#{query[i]})" }
87
- query.join(" AND ")
88
- end
89
-
90
- def crud_query_dump(data = nil)
91
- data = self.crud_get_data if !data
92
- self.crud_query_initialize(data) if !data[:query_initialized]
93
- raise Exception.new("QUERY: #{data[:query_expr]}\nPARAMS: #{data[:query_params].to_json}")
94
- end
95
-
96
- def crud_query_add_condition(data, expr, params = {})
97
- data = self.crud_get_data if !data
98
- self.crud_query_initialize(data) if !data[:query_initialized]
99
-
100
- expr = [expr] if !expr.respond_to?(:each)
101
- expr.each { |e| data[:query_expr] << e }
102
-
103
- data[:query_params].merge!(params)
104
- end
105
-
106
- def crud_query_parse_search(search)
107
- search = "(@#{search}@)"
108
- search.gsub!(/(\s+(AND|OR|NOT)\s+)/, "@) \\1 (@")
109
-
110
- # SUBSTITUTE PARAMETERS
111
- i = -1
112
- parameters = {}
113
- search.gsub!(/@(.+?)@/) do |s|
114
- i += 1
115
-
116
- key = "search_parameter_#{i}".to_sym
117
- val = $1
118
-
119
- # HANDLE LINE MARKERS
120
- if val =~ /^\^.+\$$/ then
121
- val = "#{val.gsub(/^\^(.+)\$$/, "\\1").strip}"
122
- elsif val =~ /^\^/ then
123
- val = "#{val.gsub(/^\^/, "").strip}%"
124
- elsif val =~ /\$$/ then
125
- val = "%#{val.gsub(/\$$/, "").strip}"
126
- else
127
- val = "%#{val.strip}%"
128
- end
129
-
130
- parameters[key] = val
131
- "@FIELD@ LIKE :#{key}"
132
- end
133
-
134
- [search, parameters]
135
- end
136
-
137
- def crud_handle_search(data, *fields)
138
- data = self.crud_get_data if !data
139
- self.crud_handle_extended_search(data, fields)
140
- end
141
-
142
- def crud_handle_extended_search(data, fields, externals = nil, args = nil, parameter = :search)
143
- data = self.crud_get_data if !data
144
- self.crud_query_initialize(data) if !data[:query_initialized]
145
- parameter = :search if !parameter
146
-
147
- self.crud_query_add_condition(data, "(#{self.crud_get_class(data).table_name}.#{self.crud_get_class(data).deleted_column} IS NULL)", {}) if !data[:skip_deleted]
148
-
149
- # GET QUERY
150
- args = params[parameter] if !args
151
-
152
- if args.present? then
153
- search, parameters = self.crud_query_parse_search(args)
154
-
155
- # BUILD QUERY
156
- data[:query_params].merge!(parameters)
157
- search_query = []
158
- fields.each { |field| search_query << "(#{search.gsub("@FIELD@", "#{self.crud_get_class(data).table_name}.#{field.to_s}")})" }
159
-
160
- # ADD OPTIONAL DATA
161
- if externals then
162
- externals.each do |external|
163
- external_query = ""
164
-
165
- if !external[:manual] then
166
- external_conds = []
167
- external.fetch(:fields, []).each { |external_field| external_conds << "(#{search.gsub("@FIELD@", "#{external[:table]}.#{external_field.to_s}")})" }
168
- external_field = external.fetch(:external_field, "id")
169
- external_query = "(#{external.fetch(:field, "id")} IN (SELECT #{external.fetch(:external_field, "id")} FROM #{external[:table]} WHERE #{external_conds.join(" OR ")}))"
170
- else
171
- external_conds = []
172
- raw_external_conds = []
173
-
174
- external.fetch(:fields, []).each do |external_field|
175
- external_conds << "(#{search.gsub("@FIELD@", "#{external[:table]}.#{external_field.to_s}")})"
176
- raw_external_conds << "(#{search.gsub("@FIELD@", "#{external_field.to_s}")})"
177
- end
178
-
179
- external_query = external[:query].gsub("@SEARCH@", external_conds.join(" OR "))
180
- external_query = external[:query].gsub("@RAW_SEARCH@", raw_external_conds.join(" OR "))
181
- end
182
-
183
- search_query << external_query
184
- end
185
- end
186
-
187
- self.crud_query_add_condition(data, search_query.join(" OR "))
188
- end
189
-
190
- [data[:query_expr], data[:query_params]]
191
- end
192
-
193
- def crud_handle_sorting(data, default_sorting, sort_data, sort_expression = "@PLACEHOLDER@, updated_at DESC")
194
- data = self.crud_get_data if !data
195
- data[:sort_data] = sort_data
196
- sort = self.crud_get_sort_param(default_sorting, (sort_data || {}).keys)
197
- data[:sort] = "#{sort.what}-#{sort.how.downcase}"
198
- data[:sort_order] = sort_expression.gsub("@PLACEHOLDER@", "#{sort.what} #{sort.how}")
199
- end
200
-
201
- def crud_get_form_data
202
- @record
203
- end
204
-
205
- def crud_form_header(female = false)
206
- if self.crud_get_form_data.new_record? then
207
- "Create new"
208
- else
209
- "Edit"
210
- end
211
- end
212
-
213
- def crud_form_submit_label
214
- self.crud_get_form_data.new_record? ? "Create" : "Edit"
215
- end
216
-
217
- def crud_get_page_param(key = :page, upperbound = -1)
218
- page = params[key]
219
- page = params[key].is_valid_integer? ? params[key].to_integer : 1
220
- page = 1 if page < 1
221
- page = upperbound if (upperbound > 0 && page > upperbound)
222
- page
223
- end
224
-
225
- def crud_get_sort_param(default, valids = [])
226
- sort_by = get_param(:sort_by, default)
227
- mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(sort_by)
228
- mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(default) if !mo || !((valids || []).include?(mo["what"]))
229
-
230
- sf = sort_by.split("-")
231
- rv = OpenStruct.new({:what => mo["what"], :how => mo["how"].upcase})
232
-
233
- # ADAPT SOME PARAMETERS
234
- rv.what = "status_id" if rv.what == "status"
235
-
236
- rv
237
- end
238
-
239
- def crud_calculate_data_bounds(data, records = nil, per_page = nil)
240
- data = self.crud_get_data if !data
241
- records = data[:records] if !records
242
- bounds = OpenStruct.new({:total => 0, :first => 0, :last => 0, :pages => 0, :page => 1, :per_page => 1})
243
-
244
- if records != nil && records.count > 0 then
245
- per_page = (per_page.is_valid_integer? ? per_page : records[0].class.per_page).to_integer
246
- per_page = records.count if per_page < 1
247
- bounds.total = records.count
248
- bounds.per_page = per_page
249
- bounds.pages = (bounds.total.to_f / bounds.per_page).ceil
250
- bounds.page = self.crud_get_page_param(:page, bounds.pages)
251
-
252
- base = ((bounds.page - 1) * bounds.per_page)
253
- bounds.first = base + 1
254
- bounds.last = base + bounds.per_page
255
- bounds.last = bounds.total if bounds.last > bounds.total
256
- end
257
-
258
- data[:data_bounds] = bounds
259
- end
260
-
261
- def crud_update_params
262
- blacklist = ["controller", "action", "id"]
263
- session["params-#{self.location_name}"] = (params.delete_if { |k,v| blacklist.include?(k) || params[k].is_a?(Tempfile)})
264
- end
265
-
266
- def crud_end_write_action(additional = nil, absolute = false)
267
- redirect_to self.crud_end_write_action_url(additional, absolute)
268
- end
269
-
270
- def crud_end_write_action_url(additional = nil, absolute = false)
271
- rp = {}
272
-
273
- if !absolute then
274
- rp = session["params-#{self.location_name(:index)}"] || {}
275
- rp[:action] = :index
276
- end
277
-
278
- if additional != nil then
279
- additional.each { |k, v| rp[k] = v }
280
- end
281
-
282
- url_for(rp)
283
- end
284
-
285
- def crud_delete(table, id, only_check = false)
286
- record = table.constantize.safe_find(id.to_integer)
287
-
288
- if record then
289
- if only_check then
290
- record.deletable?
291
- else
292
- record.delete
293
- end
294
- else
295
- false
296
- end
297
- end
298
- end
299
- end
300
- end
8
+ module RubyOnRails
9
+ module Helpers
10
+ module ArCrudHelper
11
+ attr_reader :data_bounds
12
+ attr_reader :record
13
+
14
+ def crud_get_data
15
+ @crud_data ||= {}
16
+ end
17
+
18
+ def crud_get_class(data = nil)
19
+ data = self.crud_get_data if !data
20
+ data[:class].constantize
21
+ end
22
+
23
+ def crud_get_records(data)
24
+ data = self.crud_get_data if !data
25
+ data[:records]
26
+ end
27
+
28
+ def crud_query_get_params(data = nil)
29
+ data = self.crud_get_data if !data
30
+ data[:query_params]
31
+ end
32
+
33
+ def crud_get_sort_order(data = nil)
34
+ data = self.crud_get_data if !data
35
+ data[:sort_order]
36
+ end
37
+
38
+ def crud_has_data?(data = nil)
39
+ data = self.crud_get_data if !data
40
+ data[:data_bounds].total > 0
41
+ end
42
+
43
+ def crud_get_pager_data(data = nil)
44
+ data = self.crud_get_data if !data
45
+ data[:pager_data]
46
+ end
47
+
48
+ def crud_get_data_bounds(data = nil)
49
+ data = self.crud_get_data if !data
50
+ data[:data_bounds]
51
+ end
52
+
53
+ def crud_set_data(data)
54
+ @crud_data = data
55
+ end
56
+
57
+ def crud_set_class(data, table = "")
58
+ data = self.crud_get_data if !data
59
+ data[:class] = table
60
+ end
61
+
62
+ def crud_set_records(data, records = nil)
63
+ data = self.crud_get_data if !data
64
+ data[:records] = records
65
+ end
66
+
67
+ def crud_finalize_read(data = nil, records = nil, parameter = :count, per_page = nil)
68
+ data = self.crud_get_data if !data
69
+ records = self.crud_get_records(data) if !records
70
+ self.crud_calculate_data_bounds(data, records, per_page || params[parameter])
71
+ data[:pager_data] = records.paginate(:page => data[:data_bounds].page, :per_page => data[:data_bounds].per_page, :total_entries => data[:data_bounds].total) if records.respond_to?(:paginate)
72
+ end
73
+
74
+ def crud_query_initialize(data = nil, force = false)
75
+ data = self.crud_get_data if !data
76
+ data[:query_expr] = [] if !data[:query_expr] || force
77
+ data[:query_params] = {} if !data[:query_params] || force
78
+ data[:query_initialized] = true
79
+ end
80
+
81
+ def crud_query_get(data = nil, query = nil)
82
+ data = self.crud_get_data if !data
83
+ self.crud_query_initialize(data) if !data[:query_initialized]
84
+ query = data[:query_expr] if !query
85
+
86
+ query.count.times { |i| query[i] = "(#{query[i]})" }
87
+ query.join(" AND ")
88
+ end
89
+
90
+ def crud_query_dump(data = nil)
91
+ data = self.crud_get_data if !data
92
+ self.crud_query_initialize(data) if !data[:query_initialized]
93
+ raise Exception.new("QUERY: #{data[:query_expr]}\nPARAMS: #{data[:query_params].to_json}")
94
+ end
95
+
96
+ def crud_query_add_condition(data, expr, params = {})
97
+ data = self.crud_get_data if !data
98
+ self.crud_query_initialize(data) if !data[:query_initialized]
99
+
100
+ expr = [expr] if !expr.respond_to?(:each)
101
+ expr.each { |e| data[:query_expr] << e }
102
+
103
+ data[:query_params].merge!(params)
104
+ end
105
+
106
+ def crud_query_parse_search(search)
107
+ search = "(@#{search}@)"
108
+ search.gsub!(/(\s+(AND|OR|NOT)\s+)/, "@) \\1 (@")
109
+
110
+ # SUBSTITUTE PARAMETERS
111
+ i = -1
112
+ parameters = {}
113
+ search.gsub!(/@(.+?)@/) do |s|
114
+ i += 1
115
+
116
+ key = "search_parameter_#{i}".to_sym
117
+ val = $1
118
+
119
+ # HANDLE LINE MARKERS
120
+ if val =~ /^\^.+\$$/ then
121
+ val = "#{val.gsub(/^\^(.+)\$$/, "\\1").strip}"
122
+ elsif val =~ /^\^/ then
123
+ val = "#{val.gsub(/^\^/, "").strip}%"
124
+ elsif val =~ /\$$/ then
125
+ val = "%#{val.gsub(/\$$/, "").strip}"
126
+ else
127
+ val = "%#{val.strip}%"
128
+ end
129
+
130
+ parameters[key] = val
131
+ "@FIELD@ LIKE :#{key}"
132
+ end
133
+
134
+ [search, parameters]
135
+ end
136
+
137
+ def crud_handle_search(data, *fields)
138
+ data = self.crud_get_data if !data
139
+ self.crud_handle_extended_search(data, fields)
140
+ end
141
+
142
+ def crud_handle_extended_search(data, fields, externals = nil, args = nil, parameter = :search)
143
+ data = self.crud_get_data if !data
144
+ self.crud_query_initialize(data) if !data[:query_initialized]
145
+ parameter = :search if !parameter
146
+
147
+ self.crud_query_add_condition(data, "(#{self.crud_get_class(data).table_name}.#{self.crud_get_class(data).deleted_column} IS NULL)", {}) if !data[:skip_deleted]
148
+
149
+ # GET QUERY
150
+ args = params[parameter] if !args
151
+
152
+ if args.present? then
153
+ search, parameters = self.crud_query_parse_search(args)
154
+
155
+ # BUILD QUERY
156
+ data[:query_params].merge!(parameters)
157
+ search_query = []
158
+ fields.each { |field| search_query << "(#{search.gsub("@FIELD@", "#{self.crud_get_class(data).table_name}.#{field.to_s}")})" }
159
+
160
+ # ADD OPTIONAL DATA
161
+ if externals then
162
+ externals.each do |external|
163
+ external_query = ""
164
+
165
+ if !external[:manual] then
166
+ external_conds = []
167
+ external.fetch(:fields, []).each { |external_field| external_conds << "(#{search.gsub("@FIELD@", "#{external[:table]}.#{external_field.to_s}")})" }
168
+ external_field = external.fetch(:external_field, "id")
169
+ external_query = "(#{external.fetch(:field, "id")} IN (SELECT #{external.fetch(:external_field, "id")} FROM #{external[:table]} WHERE #{external_conds.join(" OR ")}))"
170
+ else
171
+ external_conds = []
172
+ raw_external_conds = []
173
+
174
+ external.fetch(:fields, []).each do |external_field|
175
+ external_conds << "(#{search.gsub("@FIELD@", "#{external[:table]}.#{external_field.to_s}")})"
176
+ raw_external_conds << "(#{search.gsub("@FIELD@", "#{external_field.to_s}")})"
177
+ end
178
+
179
+ external_query = external[:query].gsub("@SEARCH@", external_conds.join(" OR "))
180
+ external_query = external[:query].gsub("@RAW_SEARCH@", raw_external_conds.join(" OR "))
181
+ end
182
+
183
+ search_query << external_query
184
+ end
185
+ end
186
+
187
+ self.crud_query_add_condition(data, search_query.join(" OR "))
188
+ end
189
+
190
+ [data[:query_expr], data[:query_params]]
191
+ end
192
+
193
+ def crud_handle_sorting(data, default_sorting, sort_data, sort_expression = "@PLACEHOLDER@, updated_at DESC")
194
+ data = self.crud_get_data if !data
195
+ data[:sort_data] = sort_data
196
+ sort = self.crud_get_sort_param(default_sorting, (sort_data || {}).keys)
197
+ data[:sort] = "#{sort.what}-#{sort.how.downcase}"
198
+ data[:sort_order] = sort_expression.gsub("@PLACEHOLDER@", "#{sort.what} #{sort.how}")
199
+ end
200
+
201
+ def crud_get_form_data
202
+ @record
203
+ end
204
+
205
+ def crud_form_header(female = false)
206
+ if self.crud_get_form_data.new_record? then
207
+ "Create new"
208
+ else
209
+ "Edit"
210
+ end
211
+ end
212
+
213
+ def crud_form_submit_label
214
+ self.crud_get_form_data.new_record? ? "Create" : "Edit"
215
+ end
216
+
217
+ def crud_get_page_param(key = :page, upperbound = -1)
218
+ page = params[key]
219
+ page = params[key].is_valid_integer? ? params[key].to_integer : 1
220
+ page = 1 if page < 1
221
+ page = upperbound if (upperbound > 0 && page > upperbound)
222
+ page
223
+ end
224
+
225
+ def crud_get_sort_param(default, valids = [])
226
+ sort_by = get_param(:sort_by, default)
227
+ mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(sort_by)
228
+ mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(default) if !mo || !((valids || []).include?(mo["what"]))
229
+
230
+ sf = sort_by.split("-")
231
+ rv = OpenStruct.new({:what => mo["what"], :how => mo["how"].upcase})
232
+
233
+ # ADAPT SOME PARAMETERS
234
+ rv.what = "status_id" if rv.what == "status"
235
+
236
+ rv
237
+ end
238
+
239
+ def crud_calculate_data_bounds(data, records = nil, per_page = nil)
240
+ data = self.crud_get_data if !data
241
+ records = data[:records] if !records
242
+ bounds = OpenStruct.new({:total => 0, :first => 0, :last => 0, :pages => 0, :page => 1, :per_page => 1})
243
+
244
+ if records != nil && records.count > 0 then
245
+ per_page = (per_page.is_valid_integer? ? per_page : records[0].class.per_page).to_integer
246
+ per_page = records.count if per_page < 1
247
+ bounds.total = records.count
248
+ bounds.per_page = per_page
249
+ bounds.pages = (bounds.total.to_f / bounds.per_page).ceil
250
+ bounds.page = self.crud_get_page_param(:page, bounds.pages)
251
+
252
+ base = ((bounds.page - 1) * bounds.per_page)
253
+ bounds.first = base + 1
254
+ bounds.last = base + bounds.per_page
255
+ bounds.last = bounds.total if bounds.last > bounds.total
256
+ end
257
+
258
+ data[:data_bounds] = bounds
259
+ end
260
+
261
+ def crud_update_params
262
+ blacklist = ["controller", "action", "id"]
263
+ session["params-#{self.location_name}"] = (params.delete_if { |k,v| blacklist.include?(k) || params[k].is_a?(Tempfile)})
264
+ end
265
+
266
+ def crud_end_write_action(additional = nil, absolute = false)
267
+ redirect_to self.crud_end_write_action_url(additional, absolute)
268
+ end
269
+
270
+ def crud_end_write_action_url(additional = nil, absolute = false)
271
+ rp = {}
272
+
273
+ if !absolute then
274
+ rp = session["params-#{self.location_name(:index)}"] || {}
275
+ rp[:action] = :index
276
+ end
277
+
278
+ if additional != nil then
279
+ additional.each { |k, v| rp[k] = v }
280
+ end
281
+
282
+ url_for(rp)
283
+ end
284
+
285
+ def crud_delete(table, id, only_check = false)
286
+ record = table.constantize.safe_find(id.to_integer)
287
+
288
+ if record then
289
+ if only_check then
290
+ record.deletable?
291
+ else
292
+ record.delete
293
+ end
294
+ else
295
+ false
296
+ end
297
+ end
298
+ end
299
+ end
300
+ end
301
301
  end