module Mongoblazer::ActiveRecord
Mongoblazer’s ActiveRecord
extension.
Public Instance Methods
configure_mongoblazer_relations!(relations, embed_type=:embeds_one)
click to toggle source
# File lib/mongoblazer/active_record.rb, line 394 def configure_mongoblazer_relations!(relations, embed_type=:embeds_one) relations.map do |em, klass_name| class_name = if klass_name "#{klass_name}Blazer" elsif embed_type == :embeds_one "#{em.to_s.camelize}Blazer" else "#{em.to_s.singularize.camelize}Blazer" end if const_defined?(class_name) <<-CODE #{embed_type} :#{em}, class_name: '#{class_name}', inverse_of: '#{mongoblazer_class_name}' accepts_nested_attributes_for :#{em} CODE else <<-CODE def #{em}=(data) write_attribute(:#{em}, data) end def #{em} data = attributes['#{em}'] if data.is_a? Array data.map{|d| OpenStruct.new(d)} elsif data.is_a? Hash OpenStruct.new(data) else data end end CODE end end end
mongoblaze!(caller=self.class)
click to toggle source
# File lib/mongoblazer/active_record.rb, line 40 def mongoblaze!(caller=self.class) if @mongoblazer_already_blazing @mongoblazer_already_blazing = false return true end self.class.recreate_mongoblazer_class! data = mongoblazer_attributes relations = {} self.class.mongoblazer_options[:embeds_one].each do |em, class_name| if related = data.delete(em) klass = class_name ? class_name.constantize : "#{em.to_s.camelize}".constantize if klass != caller related_id = related['id'] || data["#{em}_id"] if klass.mongoblazable? && related_id klass.recreate_mongoblazer_class! relations[em] = klass.find(related_id).mongoblazer_attributes(self.class) else data[em] = related.attributes end end end end self.class.mongoblazer_options[:embeds_many].each do |em, class_name| if related = data.delete(em) klass = class_name ? class_name.constantize : "#{em.to_s.singularize.camelize}".constantize if klass != caller if klass.mongoblazable? klass.recreate_mongoblazer_class! relations[em] = related.map do |r| related_id = r['id'] || data["#{em.to_s.singularize}_id"] if related_id klass.find(related_id).mongoblazer_attributes(self.class) else r.attributes end end else data[em] = related.map(&:attributes) end end end end self.class.recreate_mongoblazer_class! blazed_record = if self.mongoblazer_id.present? self.class.mongoblazer_class .where(id: self.mongoblazer_id).first else nil end if blazed_record.present? blazed_record.save!(data) else blazed_record = self.class.mongoblazer_class.create(data) @mongoblazer_already_blazing = true update_attribute :mongoblazer_id, blazed_record.id.to_s end mongoblaze_relations(blazed_record, relations) blazed_record.save! blazed_record end
mongoblaze_relations(blazed_record, relations)
click to toggle source
# File lib/mongoblazer/active_record.rb, line 111 def mongoblaze_relations(blazed_record, relations) if relations.present? self.class.recreate_mongoblazer_class! relations.each do |name, related| next if self.class.mongoblazer_options[:uploaders].include? name if related.is_a? Array blazed_record.send(name).destroy_all related.each do |r| blazed_record.send(name).build(r) end else blazed_record.send("build_#{name}", related) end end end end
mongoblazed()
click to toggle source
# File lib/mongoblazer/active_record.rb, line 10 def mongoblazed @mongoblazed ||= begin self.class.recreate_mongoblazer_class! self.class.mongoblazer_class.where(id: self.mongoblazer_id).last end end
mongoblazed_add_attribute(name, data={})
click to toggle source
# File lib/mongoblazer/active_record.rb, line 17 def mongoblazed_add_attribute(name, data={}) self.class.recreate_mongoblazer_class! instance = self.class.mongoblazer_class.where(id: self.mongoblazer_id).last instance[name] = data instance.save! @mongoblazed = instance end
mongoblazed_add_include(name, data={})
click to toggle source
Add a relation that is not in the includes options
Example: post.mongoblazed.add_include(:comments, post.comments.map(&:attributes))
Returns the mongoblazed instance with the new include.
# File lib/mongoblazer/active_record.rb, line 32 def mongoblazed_add_include(name, data={}) self.class.recreate_mongoblazer_class! relations = {name => data} mongoblaze_relations(mongoblazed, relations) mongoblazed.save! mongoblazed end
mongoblazer_attributes(caller=self.class)
click to toggle source
# File lib/mongoblazer/active_record.rb, line 132 def mongoblazer_attributes(caller=self.class) if self.class.mongoblazable? self.class.recreate_mongoblazer_class! includes = self.class.mongoblazer_options[:includes] instance = self.class.includes(includes).find(id) data = if includes instance.serializable_hash(:include => includes) else instance.attributes end if additional = self.class.mongoblazer_options[:additional_attributes] additional.each do |attribute| next if self.class.mongoblazer_options[:uploaders].include? attribute data[attribute] = instance.send(attribute) end end if uploaders = self.class.mongoblazer_options[:uploaders] uploaders.each do |uploader| data.delete(uploader) data.delete(uploader.to_s) if instance.send(uploader).present? versions = {} instance.send(uploader).versions.each do |v,u| versions[v] = u.to_s end versions.merge({default: instance.send(uploader).to_s}) end end end data[:ar_id] = data.delete('id') data.delete(caller.name.underscore.to_sym) data.delete(caller.name.pluralize.underscore.to_sym) self.class.recreate_mongoblazer_class! data else throw "#{self.class.name} is not Mongoblazable!" end end