lib/active_presenter/base.rb in active_presenter-0.0.1 vs lib/active_presenter/base.rb in active_presenter-0.0.2
- old
+ new
@@ -56,11 +56,11 @@
end
# Set the attributes of the presentable instances using the type_attribute form (i.e. user_login => 'james')
#
def attributes=(attrs)
- attrs.each { |k,v| send("#{k}=", v) }
+ attrs.each { |k,v| send("#{k}=", v) unless attribute_protected?(k)}
end
# Makes sure that the presenter is accurate about responding to presentable's attributes, even though they are handled by method_missing.
#
def respond_to?(method)
@@ -143,11 +143,12 @@
method_name.to_s.starts_with?(attribute_prefix(type))
end
end
def presented_attribute?(method_name)
- !presentable_for(method_name).nil?
+ p = presentable_for(method_name)
+ !p.nil? && send(p).respond_to?(flatten_attribute_name(method_name,p))
end
def flatten_attribute_name(name, type)
name.to_s.gsub(/^#{attribute_prefix(type)}/, '')
end
@@ -158,8 +159,14 @@
def merge_errors(presented_inst, type)
presented_inst.errors.each do |att,msg|
errors.add(attribute_prefix(type)+att, msg)
end
+ end
+
+ def attribute_protected?(name)
+ presentable = presentable_for(name)
+ flat_attribute = {flatten_attribute_name(name, presentable) => ''} #remove_att... normally takes a hash, so we use a ''
+ presentable.to_s.classify.constantize.new.send(:remove_attributes_protected_from_mass_assignment, flat_attribute).empty?
end
end
end