Module: Mongoblazer::ActiveRecord::ClassMethods
- Defined in:
- lib/mongoblazer/active_record.rb
Instance Method Summary collapse
- #belongs_to(name, options = {}) ⇒ Object
- #find_blazed(id) ⇒ Object
- #has_and_belongs_to_many(name, options = {}) ⇒ Object
- #has_many(name, options = {}) ⇒ Object
- #has_one(name, options = {}) ⇒ Object
-
#mongoblazable? ⇒ Boolean
Is this model Mongoblazable?.
-
#mongoblazer_additional_attributes(options = {}) ⇒ Object
Defines additional attributes (e.g. not in db) to be merged in the mongodb document.
- #mongoblazer_class ⇒ Object
- #mongoblazer_class_name ⇒ Object
-
#mongoblazer_default_scope(options = {}) ⇒ Object
Defines default scope to find the matching mongodb document.
-
#mongoblazer_includes(options = {}) ⇒ Object
Defines relation includes to be merged in the mongodb document.
-
#mongoblazer_index_fields(options = {}) ⇒ Object
Defines indexes on the blazer model.
-
#mongoblazer_init(options) ⇒ Object
Initialize Mongoblazer wit some options: includes: relations to include default_scope: the default scope for the blazed model indexes: fields to index in the blazed model embeds_one: model to embed one of embeds_many: model to embed many of.
- #mongoblazer_options ⇒ Object
- #recreate_mongoblazer_class!(called_from_parent = false) ⇒ Object
Instance Method Details
#belongs_to(name, options = {}) ⇒ Object
222 223 224 225 |
# File 'lib/mongoblazer/active_record.rb', line 222 def belongs_to(name, ={}) mongoblazer_init embeds_one: {name => [:class_name]} super end |
#find_blazed(id) ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/mongoblazer/active_record.rb', line 207 def find_blazed(id) ar_instance = select("#{self.table_name}.mongoblazer_id").find(id) recreate_mongoblazer_class! mongoblazer_class.find(ar_instance.mongoblazer_id) end |
#has_and_belongs_to_many(name, options = {}) ⇒ Object
237 238 239 240 |
# File 'lib/mongoblazer/active_record.rb', line 237 def has_and_belongs_to_many(name, ={}) mongoblazer_init embeds_many: {name => [:class_name]} super end |
#has_many(name, options = {}) ⇒ Object
232 233 234 235 |
# File 'lib/mongoblazer/active_record.rb', line 232 def has_many(name, ={}) mongoblazer_init embeds_many: {name => [:class_name]} super end |
#has_one(name, options = {}) ⇒ Object
227 228 229 230 |
# File 'lib/mongoblazer/active_record.rb', line 227 def has_one(name, ={}) mongoblazer_init embeds_one: {name => [:class_name]} super end |
#mongoblazable? ⇒ Boolean
Is this model Mongoblazable?
218 219 220 |
# File 'lib/mongoblazer/active_record.rb', line 218 def mongoblazable? .present? end |
#mongoblazer_additional_attributes(options = {}) ⇒ Object
Defines additional attributes (e.g. not in db) to be merged in the mongodb document.
274 275 276 |
# File 'lib/mongoblazer/active_record.rb', line 274 def mongoblazer_additional_attributes(={}) mongoblazer_init additional_attributes: end |
#mongoblazer_class ⇒ Object
328 329 330 |
# File 'lib/mongoblazer/active_record.rb', line 328 def mongoblazer_class mongoblazer_class_name.constantize end |
#mongoblazer_class_name ⇒ Object
332 333 334 |
# File 'lib/mongoblazer/active_record.rb', line 332 def mongoblazer_class_name "#{self.name}Blazer" end |
#mongoblazer_default_scope(options = {}) ⇒ Object
Defines default scope to find the matching mongodb document.
Syntax same as where() in AR
259 260 261 |
# File 'lib/mongoblazer/active_record.rb', line 259 def mongoblazer_default_scope(={}) mongoblazer_init default_scope: end |
#mongoblazer_includes(options = {}) ⇒ Object
Defines relation includes to be merged in the mongodb document.
Syntax same as eager loading syntax in AR:
http://guides.rubyonrails.org/.html#eager-loading-associations
249 250 251 |
# File 'lib/mongoblazer/active_record.rb', line 249 def mongoblazer_includes(={}) mongoblazer_init includes: end |
#mongoblazer_index_fields(options = {}) ⇒ Object
Defines indexes on the blazer model.
266 267 268 |
# File 'lib/mongoblazer/active_record.rb', line 266 def mongoblazer_index_fields(={}) mongoblazer_init indexes: end |
#mongoblazer_init(options) ⇒ Object
Initialize Mongoblazer wit some options:
includes: relations to include
default_scope: the default scope for the blazed model
indexes: fields to index in the blazed model
embeds_one: model to embed one of
embeds_many: model to embed many of
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/mongoblazer/active_record.rb', line 286 def mongoblazer_init() unless @mongoblazer_options @mongoblazer_options = {} @mongoblazer_options[:indexes] = ::ActiveRecord::Base.connection.indexes(self.table_name).map do |ind| {ind => 1} end @mongoblazer_options[:uploaders] = [] @mongoblazer_options[:embeds_one] = {} @mongoblazer_options[:embeds_many] = {} end if one = .delete(:embeds_one) @mongoblazer_options[:embeds_one][one.keys.first] = one.values.first end if many = .delete(:embeds_many) @mongoblazer_options[:embeds_many][many.keys.first] = many.values.first end if uploader = .delete(:uploaders) @mongoblazer_options[:uploaders] << uploader end @mongoblazer_options.merge! create_mongoblazer_class! end |
#mongoblazer_options ⇒ Object
318 319 320 321 322 323 324 325 326 |
# File 'lib/mongoblazer/active_record.rb', line 318 def if defined?(@mongoblazer_options) @mongoblazer_options elsif superclass.respond_to?(:mongoblazer_options) superclass. || { } else { } end end |
#recreate_mongoblazer_class!(called_from_parent = false) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/mongoblazer/active_record.rb', line 336 def recreate_mongoblazer_class!(called_from_parent=false) create_mongoblazer_class! unless called_from_parent begin configuration[:embeds_one].each do |rel| "#{rel.to_s.camelize}".constantize.recreate_mongoblazer_class!(true) end rescue NameError end begin configuration[:embeds_many].each do |rel| "#{rel.to_s.singularize.camelize}".constantize.recreate_mongoblazer_class!(true) end rescue NameError end end end |