Module: HasAlterEgo::ActiveRecordAdapater::ClassMethods
- Defined in:
- lib/has_alter_ego.rb
Instance Method Summary collapse
- #assign_attributes(obj, yml) ⇒ Object
- #get_yml ⇒ Object
- #has_alter_ego(opts = {}) ⇒ Object
- #parse_yml ⇒ Object
- #parse_yml_for(id, reload = false) ⇒ Object
-
#reserve_space(space) ⇒ Object
Reserve the first n IDs for stubbed objects.
Instance Method Details
#assign_attributes(obj, yml) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/has_alter_ego.rb', line 67 def assign_attributes obj, yml reflections = self.reflect_on_all_associations yml.keys.each do |attr| if obj.respond_to?(attr) obj.send(attr+"=" , yml[attr]) else # Handle smart associations reflections.select{|r| attr.start_with?(r.name.to_s+"_by_")}.each do |r| finder = [:has_one, :belongs_to].include?(r.macro) ? "find" : "find_all" if attr.index("_and_") objects = r.klass.send(attr.gsub(r.name.to_s, finder), *(yml[attr])) else objects = r.klass.send(attr.gsub(r.name.to_s, finder), yml[attr]) end obj.send(r.name.to_s+"=", objects) if objects.present? end end end end |
#get_yml ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/has_alter_ego.rb', line 87 def get_yml filename = File.join(Rails.root.to_s, "db", "fixtures", "alter_egos", self.table_name + ".yml") return {} unless File.exists?(filename) yml = File.open(filename) do |yf| YAML::load( yf ) end yml end |
#has_alter_ego(opts = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/has_alter_ego.rb', line 10 def has_alter_ego opts = {} opts.reverse_merge!({:reserved_space => 1000}) class_eval do has_one :alter_ego, :as => :alter_ego_object alias_method :save_without_alter_ego, :save alias_method :destroy_without_alter_ego, :destroy send :include, InstanceMethods reserve_space(opts[:reserved_space]) parse_yml end end |
#parse_yml ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/has_alter_ego.rb', line 34 def parse_yml @yml = get_yml @db_objects = self.find_all_by_id(@yml.keys, :include => :alter_ego) || [] @yml.keys.each do |o| parse_yml_for o end @yml end |
#parse_yml_for(id, reload = false) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/has_alter_ego.rb', line 43 def parse_yml_for id, reload = false db_object = reload ? (self.find(id) rescue nil) : @db_objects.select{|o| o.id == id}.first if db_object raise "There is already a #{db_object.class} with id #{db_object.id} in the database." unless db_object.has_alter_ego? if db_object.alter_ego.state == 'default' assign_attributes db_object, @yml[id] db_object.on_seed(@yml[id]) db_object.save_without_alter_ego end else # Check for destroyed alter_egos alter_ego = AlterEgo.find_by_alter_ego_object_id_and_alter_ego_object_type(id, self.name) return if alter_ego.try(:state) == "destroyed" db_object = self.new db_object.id = id assign_attributes db_object, @yml[id] db_object.build_alter_ego db_object.alter_ego.state = 'default' db_object.on_seed(@yml[id]) db_object.save_without_alter_ego end end |
#reserve_space(space) ⇒ Object
Reserve the first n IDs for stubbed objects
24 25 26 27 28 29 30 31 32 |
# File 'lib/has_alter_ego.rb', line 24 def reserve_space space return if self.last and self.last.id >= space o = self.new o.id= space o.save_without_alter_ego o.delete return end |