lib/dir_model/model/relations.rb in dir_model-0.6.1 vs lib/dir_model/model/relations.rb in dir_model-0.6.2

- old
+ new

@@ -18,12 +18,12 @@ # Appends model to the parent and returns it # # @return [Model] return the child if it is valid, otherwise returns nil def append_dir_model(source_path, options={}) - relation_name = self.class.has_one_relationship.keys.first - _options = self.class.has_one_relationship.values.first + relation_name = options[:relation_name] + _options = options[:relation_options] related_class = _options[:dir_model_class] foreign_key = _options[:foreign_key] foreign_value = self.send(foreign_key) related_dir_model = related_class.new(source_path, @@ -77,20 +77,22 @@ # Defines a relationship between a dir model # # @param [Symbol] relation_name the name of the relation # @param [DirModel::Import] dir_model_class class of the relation def has_one(relation_name, dir_model_class, options) - raise "for now, DirModel's has_one may only be called once" if @_has_one_relationship.present? - relation_name = relation_name.to_sym merge_has_one_relationship(relation_name => { dir_model_class: dir_model_class }.merge(options)) define_method(:has_one?) do true end + define_method(:has_one) do + self.class.has_one_relationship + end + define_method("#{relation_name}=") do |value| instance_variable_set("@#{relation_name}", value) end define_method(relation_name) do @@ -107,10 +109,9 @@ # @param [Symbol] relation_name the name of the relation # @param [DirModel::Import] dir_model_class class of the relation # @param [Hash] basically for set :foreign_key def has_many(relation_name, dir_model_class, options) raise "for now, DirModel's has_many may only be called once" if @_has_many_relationship.present? - relation_name = relation_name.to_sym merge_has_many_relationship(relation_name => { dir_model_class: dir_model_class }.merge(options)) define_method(:has_many?) do