Module: Cowtech::RubyOnRails::Helpers::ArCrudHelper
- Defined in:
- app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb
Instance Attribute Summary collapse
-
#data_bounds ⇒ Object
readonly
Returns the value of attribute data_bounds.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #crud_calculate_data_bounds(data, records = nil, per_page = nil) ⇒ Object
- #crud_delete(table, id, only_check = false) ⇒ Object
- #crud_end_write_action(additional = nil, absolute = false) ⇒ Object
- #crud_end_write_action_url(additional = nil, absolute = false) ⇒ Object
- #crud_finalize_read(data = nil, records = nil, parameter = :count, per_page = nil) ⇒ Object
- #crud_form_header(female = false) ⇒ Object
- #crud_form_submit_label ⇒ Object
- #crud_get_class(data = nil) ⇒ Object
- #crud_get_data ⇒ Object
- #crud_get_data_bounds(data = nil) ⇒ Object
- #crud_get_form_data ⇒ Object
- #crud_get_page_param(key = :page, upperbound = -1)) ⇒ Object
- #crud_get_pager_data(data = nil) ⇒ Object
- #crud_get_records(data) ⇒ Object
- #crud_get_sort_order(data = nil) ⇒ Object
- #crud_get_sort_param(default, valids = []) ⇒ Object
- #crud_handle_extended_search(data, fields, externals = nil, args = nil, parameter = :search) ⇒ Object
- #crud_handle_search(data, *fields) ⇒ Object
- #crud_handle_sorting(data, default_sorting, sort_data, sort_expression = "@PLACEHOLDER@, updated_at DESC") ⇒ Object
- #crud_has_data?(data = nil) ⇒ Boolean
- #crud_query_add_condition(data, expr, params = {}) ⇒ Object
- #crud_query_dump(data = nil) ⇒ Object
- #crud_query_get(data = nil, query = nil) ⇒ Object
- #crud_query_get_params(data = nil) ⇒ Object
- #crud_query_initialize(data = nil, force = false) ⇒ Object
- #crud_query_parse_search(search) ⇒ Object
- #crud_set_class(data, table = "") ⇒ Object
- #crud_set_data(data) ⇒ Object
- #crud_set_records(data, records = nil) ⇒ Object
- #crud_update_params ⇒ Object
Instance Attribute Details
#data_bounds ⇒ Object (readonly)
Returns the value of attribute data_bounds.
11 12 13 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 11 def data_bounds @data_bounds end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
12 13 14 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 12 def record @record end |
Instance Method Details
#crud_calculate_data_bounds(data, records = nil, per_page = nil) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 239 def crud_calculate_data_bounds(data, records = nil, per_page = nil) data = self.crud_get_data if !data records = data[:records] if !records bounds = OpenStruct.new({total: 0, first: 0, last: 0, pages: 0, page: 1, per_page: 1}) if records != nil && records.count > 0 then per_page = (per_page.is_valid_integer? ? per_page : records[0].class.per_page).to_integer per_page = records.count if per_page < 1 bounds.total = records.count bounds.per_page = per_page bounds.pages = (bounds.total.to_f / bounds.per_page).ceil bounds.page = self.crud_get_page_param(:page, bounds.pages) base = ((bounds.page - 1) * bounds.per_page) bounds.first = base + 1 bounds.last = base + bounds.per_page bounds.last = bounds.total if bounds.last > bounds.total end data[:data_bounds] = bounds end |
#crud_delete(table, id, only_check = false) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 285 def crud_delete(table, id, only_check = false) record = table.constantize.safe_find(id.to_integer) if record then if only_check then record.deletable? else record.delete end else false end end |
#crud_end_write_action(additional = nil, absolute = false) ⇒ Object
266 267 268 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 266 def crud_end_write_action(additional = nil, absolute = false) redirect_to self.crud_end_write_action_url(additional, absolute) end |
#crud_end_write_action_url(additional = nil, absolute = false) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 270 def crud_end_write_action_url(additional = nil, absolute = false) rp = {} if !absolute then rp = session["params-#{self.location_name(:index)}"] || {} rp[:action] = :index end if additional != nil then additional.each { |k, v| rp[k] = v } end url_for(rp) end |
#crud_finalize_read(data = nil, records = nil, parameter = :count, per_page = nil) ⇒ Object
67 68 69 70 71 72 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 67 def crud_finalize_read(data = nil, records = nil, parameter = :count, per_page = nil) data = self.crud_get_data if !data records = self.crud_get_records(data) if !records self.crud_calculate_data_bounds(data, records, per_page || params[parameter]) 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) end |
#crud_form_header(female = false) ⇒ Object
205 206 207 208 209 210 211 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 205 def crud_form_header(female = false) if self.crud_get_form_data.new_record? then "Create new" else "Edit" end end |
#crud_form_submit_label ⇒ Object
213 214 215 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 213 def crud_form_submit_label self.crud_get_form_data.new_record? ? "Create" : "Edit" end |
#crud_get_class(data = nil) ⇒ Object
18 19 20 21 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 18 def crud_get_class(data = nil) data = self.crud_get_data if !data data[:class].constantize end |
#crud_get_data ⇒ Object
14 15 16 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 14 def crud_get_data @crud_data ||= {} end |
#crud_get_data_bounds(data = nil) ⇒ Object
48 49 50 51 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 48 def crud_get_data_bounds(data = nil) data = self.crud_get_data if !data data[:data_bounds] end |
#crud_get_form_data ⇒ Object
201 202 203 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 201 def crud_get_form_data @record end |
#crud_get_page_param(key = :page, upperbound = -1)) ⇒ Object
217 218 219 220 221 222 223 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 217 def crud_get_page_param(key = :page, upperbound = -1) page = params[key] page = params[key].is_valid_integer? ? params[key].to_integer : 1 page = 1 if page < 1 page = upperbound if (upperbound > 0 && page > upperbound) page end |
#crud_get_pager_data(data = nil) ⇒ Object
43 44 45 46 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 43 def crud_get_pager_data(data = nil) data = self.crud_get_data if !data data[:pager_data] end |
#crud_get_records(data) ⇒ Object
23 24 25 26 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 23 def crud_get_records(data) data = self.crud_get_data if !data data[:records] end |
#crud_get_sort_order(data = nil) ⇒ Object
33 34 35 36 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 33 def crud_get_sort_order(data = nil) data = self.crud_get_data if !data data[:sort_order] end |
#crud_get_sort_param(default, valids = []) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 225 def crud_get_sort_param(default, valids = []) sort_by = get_param(:sort_by, default) mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(sort_by) mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(default) if !mo || !((valids || []).include?(mo["what"])) sf = sort_by.split("-") rv = OpenStruct.new({what: mo["what"], how: mo["how"].upcase}) # Adapt some parameters rv.what = "status_id" if rv.what == "status" rv end |
#crud_handle_extended_search(data, fields, externals = nil, args = nil, parameter = :search) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 142 def crud_handle_extended_search(data, fields, externals = nil, args = nil, parameter = :search) data = self.crud_get_data if !data self.crud_query_initialize(data) if !data[:query_initialized] parameter = :search if !parameter 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] # Get query args = params[parameter] if !args if args.present? then search, parameters = self.crud_query_parse_search(args) # Build query data[:query_params].merge!(parameters) search_query = [] fields.each { |field| search_query << "(#{search.gsub("@FIELD@", "#{self.crud_get_class(data).table_name}.#{field.to_s}")})" } # Add optional data if externals then externals.each do |external| external_query = "" if !external[:manual] then external_conds = [] external.fetch(:fields, []).each { |external_field| external_conds << "(#{search.gsub("@FIELD@", "#{external[:table]}.#{external_field.to_s}")})" } external_field = external.fetch(:external_field, "id") external_query = "(#{external.fetch(:field, "id")} IN (SELECT #{external.fetch(:external_field, "id")} FROM #{external[:table]} WHERE #{external_conds.join(" OR ")}))" else external_conds = [] raw_external_conds = [] external.fetch(:fields, []).each do |external_field| external_conds << "(#{search.gsub("@FIELD@", "#{external[:table]}.#{external_field.to_s}")})" raw_external_conds << "(#{search.gsub("@FIELD@", "#{external_field.to_s}")})" end external_query = external[:query].gsub("@SEARCH@", external_conds.join(" OR ")) external_query = external[:query].gsub("@RAW_SEARCH@", raw_external_conds.join(" OR ")) end search_query << external_query end end self.crud_query_add_condition(data, search_query.join(" OR ")) end [data[:query_expr], data[:query_params]] end |
#crud_handle_search(data, *fields) ⇒ Object
137 138 139 140 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 137 def crud_handle_search(data, *fields) data = self.crud_get_data if !data self.crud_handle_extended_search(data, fields) end |
#crud_handle_sorting(data, default_sorting, sort_data, sort_expression = "@PLACEHOLDER@, updated_at DESC") ⇒ Object
193 194 195 196 197 198 199 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 193 def crud_handle_sorting(data, default_sorting, sort_data, sort_expression = "@PLACEHOLDER@, updated_at DESC") data = self.crud_get_data if !data data[:sort_data] = sort_data sort = self.crud_get_sort_param(default_sorting, (sort_data || {}).keys) data[:sort] = "#{sort.what}-#{sort.how.downcase}" data[:sort_order] = sort_expression.gsub("@PLACEHOLDER@", "#{sort.what} #{sort.how}") end |
#crud_has_data?(data = nil) ⇒ Boolean
38 39 40 41 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 38 def crud_has_data?(data = nil) data = self.crud_get_data if !data data[:data_bounds].total > 0 end |
#crud_query_add_condition(data, expr, params = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 96 def crud_query_add_condition(data, expr, params = {}) data = self.crud_get_data if !data self.crud_query_initialize(data) if !data[:query_initialized] expr = [expr] if !expr.respond_to?(:each) expr.each { |e| data[:query_expr] << e } data[:query_params].merge!(params) end |
#crud_query_dump(data = nil) ⇒ Object
90 91 92 93 94 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 90 def crud_query_dump(data = nil) data = self.crud_get_data if !data self.crud_query_initialize(data) if !data[:query_initialized] raise Exception.new("QUERY: #{data[:query_expr]}\nPARAMS: #{data[:query_params].to_json}") end |
#crud_query_get(data = nil, query = nil) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 81 def crud_query_get(data = nil, query = nil) data = self.crud_get_data if !data self.crud_query_initialize(data) if !data[:query_initialized] query = data[:query_expr] if !query query.count.times { |i| query[i] = "(#{query[i]})" } query.join(" AND ") end |
#crud_query_get_params(data = nil) ⇒ Object
28 29 30 31 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 28 def crud_query_get_params(data = nil) data = self.crud_get_data if !data data[:query_params] end |
#crud_query_initialize(data = nil, force = false) ⇒ Object
74 75 76 77 78 79 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 74 def crud_query_initialize(data = nil, force = false) data = self.crud_get_data if !data data[:query_expr] = [] if !data[:query_expr] || force data[:query_params] = {} if !data[:query_params] || force data[:query_initialized] = true end |
#crud_query_parse_search(search) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 106 def crud_query_parse_search(search) search = "(@#{search}@)" search.gsub!(/(\s+(AND|OR|NOT)\s+)/, "@) \\1 (@") # Substitute parameters i = -1 parameters = {} search.gsub!(/@(.+?)@/) do |s| i += 1 key = "search_parameter_#{i}".to_sym val = $1 # HANDLE LINE MARKERS if val =~ /^\^.+\$$/ then val = "#{val.gsub(/^\^(.+)\$$/, "\\1").strip}" elsif val =~ /^\^/ then val = "#{val.gsub(/^\^/, "").strip}%" elsif val =~ /\$$/ then val = "%#{val.gsub(/\$$/, "").strip}" else val = "%#{val.strip}%" end parameters[key] = val "@FIELD@ LIKE :#{key}" end [search, parameters] end |
#crud_set_class(data, table = "") ⇒ Object
57 58 59 60 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 57 def crud_set_class(data, table = "") data = self.crud_get_data if !data data[:class] = table end |
#crud_set_data(data) ⇒ Object
53 54 55 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 53 def crud_set_data(data) @crud_data = data end |
#crud_set_records(data, records = nil) ⇒ Object
62 63 64 65 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 62 def crud_set_records(data, records = nil) data = self.crud_get_data if !data data[:records] = records end |
#crud_update_params ⇒ Object
261 262 263 264 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb', line 261 def crud_update_params blacklist = ["controller", "action", "id"] session["params-#{self.location_name}"] = (params.delete_if { |k,v| blacklist.include?(k) || params[k].is_a?(Tempfile)}) end |