Module: Cowtech::RubyOnRails::Helpers::MongoidCrudHelper
- Defined in:
- app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb
Instance Attribute Summary collapse
-
#mongo_bounds ⇒ Object
Returns the value of attribute mongo_bounds.
-
#mongo_class ⇒ Object
Returns the value of attribute mongo_class.
-
#mongo_pager ⇒ Object
Returns the value of attribute mongo_pager.
-
#mongo_query ⇒ Object
Returns the value of attribute mongo_query.
-
#mongo_records ⇒ Object
Returns the value of attribute mongo_records.
-
#mongo_sort_order ⇒ Object
Returns the value of attribute mongo_sort_order.
-
#records ⇒ Object
Returns the value of attribute records.
Instance Method Summary collapse
- #mongo_add_query_conditions(args = {}) ⇒ Object
- #mongo_calculate_bounds(args = {}) ⇒ Object
- #mongo_delete(args = {}) ⇒ Object
- #mongo_dump_query(args = {}) ⇒ Object
- #mongo_end_write_action(args = {}) ⇒ Object
- #mongo_end_write_action_url(args = {}) ⇒ Object
- #mongo_exists?(args) ⇒ Boolean
- #mongo_fetch_data(args = {}) ⇒ Object
- #mongo_form_header(args = {}) ⇒ Object
- #mongo_form_submit_label(args = {}) ⇒ Object
- #mongo_get_page_param(args = {}) ⇒ Object
- #mongo_get_sort_param(args = {}) ⇒ Object
- #mongo_handle_search(args = {}) ⇒ Object
- #mongo_handle_sorting(args = {}) ⇒ Object
- #mongo_has_data?(args = {}) ⇒ Boolean
- #mongo_is_available?(args = {}) ⇒ Boolean
- #mongo_parse_search(search) ⇒ Object
- #mongo_reset_query(args = {}) ⇒ Object
- #mongo_setup(args = {}) ⇒ Object
- #mongo_update_params(args = {}) ⇒ Object
- #mongo_update_params_black_list(args = {}) ⇒ Object
Instance Attribute Details
#mongo_bounds ⇒ Object
Returns the value of attribute mongo_bounds.
14 15 16 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 14 def mongo_bounds @mongo_bounds end |
#mongo_class ⇒ Object
Returns the value of attribute mongo_class.
11 12 13 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 11 def mongo_class @mongo_class end |
#mongo_pager ⇒ Object
Returns the value of attribute mongo_pager.
17 18 19 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 17 def mongo_pager @mongo_pager end |
#mongo_query ⇒ Object
Returns the value of attribute mongo_query.
15 16 17 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 15 def mongo_query @mongo_query end |
#mongo_records ⇒ Object
Returns the value of attribute mongo_records.
16 17 18 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 16 def mongo_records @mongo_records end |
#mongo_sort_order ⇒ Object
Returns the value of attribute mongo_sort_order.
13 14 15 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 13 def mongo_sort_order @mongo_sort_order end |
#records ⇒ Object
Returns the value of attribute records.
12 13 14 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 12 def records @records end |
Instance Method Details
#mongo_add_query_conditions(args = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 67 def mongo_add_query_conditions(args = {}) (args[:conditions] || []).ensure_array.each do |condition| if condition.is_a?(Hash) then condition.each_pair do |key, val| if key == "$or" then @mongo_query = @mongo_query.any_of(val) # TODO: This doesn't work as expected. See: https://github.com/mongoid/mongoid/issues/569 else @mongo_query = @mongo_query.where({key => val}) end end end end @mongo_query = yield(@mongo_query) if block_given? @mongo_query end |
#mongo_calculate_bounds(args = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 32 def mongo_calculate_bounds(args = {}) if @mongo_records.present? then @mongo_bounds[:total] = @mongo_records.count @mongo_bounds[:per_page] = (args[:per_page].is_integer? ? args[:per_page] : @mongo_records.first.class.per_page).to_integer @mongo_bounds[:pages] = (@mongo_bounds[:total].to_f / @mongo_bounds[:per_page]).ceil if @mongo_bounds[:per_page] > 0 then @mongo_bounds[:page] = self.mongo_get_page_param(upperbound: @mongo_bounds[:pages]) base = ((@mongo_bounds[:page] - 1) * @mongo_bounds[:per_page]) @mongo_bounds[:first] = base + 1 @mongo_bounds[:last] = [base + @mongo_bounds[:per_page], @mongo_bounds[:total]].min else @mongo_bounds.merge!(pages: 1, page: 1, first: 1, last: @mongo_bounds[:total], per_page: @mongo_bounds[:total]) end end @mongo_bounds end |
#mongo_delete(args = {}) ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 182 def mongo_delete(args = {}) record = (args[:class] || @mongo_class).safe_find(args[:id]) if record then args[:only_check] ? record.deletable? : record.delete(args[:definitive]) else false end end |
#mongo_dump_query(args = {}) ⇒ Object
63 64 65 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 63 def mongo_dump_query(args = {}) raise Exception.new("QUERY:\n#{@mongo_query.inspect}") end |
#mongo_end_write_action(args = {}) ⇒ Object
212 213 214 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 212 def mongo_end_write_action(args = {}) redirect_to self.mongo_end_write_action_url(args) end |
#mongo_end_write_action_url(args = {}) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 216 def mongo_end_write_action_url(args = {}) rp = {} if !args[:absolute] then rp = session["params-#{self.location_name(:index)}"] || {} rp[:action] = :index end if args[:additional].is_a?(Hash) then args[:additional].each { |k, v| rp[k] = v } end url_for(rp) end |
#mongo_exists?(args) ⇒ Boolean
192 193 194 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 192 def mongo_exists?(args) args[:class].not_deleted.where(args[:conditions]).count > 0 end |
#mongo_fetch_data(args = {}) ⇒ Object
51 52 53 54 55 56 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 51 def mongo_fetch_data(args = {}) @mongo_records = @mongo_query.order_by(@mongo_sort_order) self.mongo_calculate_bounds(args.reverse_merge(per_page: (args[:per_page] || @mongo_per_page || params[args[:parameter] || :count]))) @records = @mongo_records.skip(@mongo_bounds[:first] - 1).limit(@mongo_bounds[:per_page]) @mongo_pager = WillPaginate::Collection.new(@mongo_bounds[:page], @mongo_bounds[:per_page], @mongo_bounds[:total]) end |
#mongo_form_header(args = {}) ⇒ Object
160 161 162 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 160 def mongo_form_header(args = {}) args[:record].try(:new_record?) ? "Create" : "Edit" end |
#mongo_form_submit_label(args = {}) ⇒ Object
164 165 166 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 164 def mongo_form_submit_label(args = {}) args[:record].try(:new_record?) ? "Create" : "Edit" end |
#mongo_get_page_param(args = {}) ⇒ Object
168 169 170 171 172 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 168 def mongo_get_page_param(args = {}) page = [params[args[:parameter] || :page].to_integer, 1].max page = [page, args[:upperbound]].min if args[:upperbound].to_integer > 0 page end |
#mongo_get_sort_param(args = {}) ⇒ Object
174 175 176 177 178 179 180 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 174 def mongo_get_sort_param(args = {}) if /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(params[args[:param] || :sort_by]) && (args[:valids] || []).include?($~["what"]) then [$~["what"].to_sym, $~["how"].downcase.to_sym] else args[:default] || [:created_at, :desc] end end |
#mongo_handle_search(args = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 116 def mongo_handle_search(args = {}) parameter = args[:parameter] || :search # Get the query search_query = args[:query] || params[parameter] if search_query.present? then expr = self.mongo_parse_search(search_query) # Build the query or_query = [] (args[:fields] || []).each do |field| or_query << {field.to_sym => Regexp.new(expr, Regexp::EXTENDED | Regexp::MULTILINE | Regexp::IGNORECASE)} end # Now add external fields (args[:external] || []).each do |external| external_query = external[:class].not_deleted eor_query = [] (external[:fields] || []).each do |field| eor_query << {field.to_sym => Regexp.new(expr, Regexp::EXTENDED | Regexp::MULTILINE | Regexp::IGNORECASE)} end ids = external_query.any_of(eor_query).only(:id).all.collect { |r| r.id } or_query << {external[:foreign_key].in => ids} if ids.count > 0 end @mongo_query = @mongo_query.any_of(or_query) end @mongo_query end |
#mongo_handle_sorting(args = {}) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 148 def mongo_handle_sorting(args = {}) order = args[:order] || [:current, [:updated_at, :desc]] # Get current request sort order and then replace it into the sort fields current = self.mongo_get_sort_param(default: args[:default]) current_index = order.index(:current) order[current_index] = current if current_index # Assign data @mongo_sort_order = order end |
#mongo_has_data?(args = {}) ⇒ Boolean
28 29 30 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 28 def mongo_has_data?(args = {}) @mongo_bounds[:total] > 0 end |
#mongo_is_available?(args = {}) ⇒ Boolean
196 197 198 199 200 201 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 196 def mongo_is_available?(args = {}) rv = self.setup_json_response(:validator) rv["success"] = true rv["valid"] = (self.mongo_exists?(args) == (args[:must_exists] || false)) args[:internal] ? rv : self.custom_respond_with(rv.to_json) end |
#mongo_parse_search(search) ⇒ Object
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 112 113 114 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 84 def mongo_parse_search(search) if search.present? then # Operate on parenthesis. If unbalanced, no substitution is done if search.gsub(/[^(]/mi, "").strip.length == search.gsub(/[^)]/mi, "").strip.length then # Split token search = search.split(/(\s(AND|OR)\s)|([\(\)])/).select{ |t| !t.empty? && t !~ /^(AND|OR)$/} # Replace tokens search = search.collect { |token| case token when /[\(\)]/ then # No replace token when /\sAND\s/ then "(.+)" when /\sOR\s/ then "|" when /^\^(.+)/ then "(^(#{Regexp.escape($1)}))" when /(.+)\$$/ then "((#{Regexp.escape($1)})$)" else "(" + Regexp.escape(token) + ")" end }.join("") else search = Regexp.quote(search) end end search end |
#mongo_reset_query(args = {}) ⇒ Object
58 59 60 61 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 58 def mongo_reset_query(args = {}) klass = args[:class] || @mongo_class klass ? (args[:also_deleted] ? klass.where : klass.not_deleted) : nil end |
#mongo_setup(args = {}) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 19 def mongo_setup(args = {}) @mongo_class = args[:class] if args[:class] @mongo_class = @mongo_class.constantize if @mongo_class.is_a?(String) @mongo_records = [] @mongo_sort_order = [[:_id, :asc]] @mongo_bounds = {total: 0, first: 1, last: 0, pages: 1, page: 1, per_page: 1} @mongo_query = self.mongo_reset_query(also_deleted: args[:also_deleted]) if @mongo_class end |
#mongo_update_params(args = {}) ⇒ Object
207 208 209 210 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 207 def mongo_update_params(args = {}) blacklist = self.mongo_update_params_black_list(args) session["params-#{self.location_name}"] = (params.delete_if { |k,v| blacklist.include?(k) || params[k].is_a?(Tempfile) || params[k].blank?}) end |
#mongo_update_params_black_list(args = {}) ⇒ Object
203 204 205 |
# File 'app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb', line 203 def mongo_update_params_black_list(args = {}) ["controller", "action", "id", "subdomain"] + (args[:additional] || []) end |