lib/active_presenter/base.rb in active_presenter-1.2.0 vs lib/active_presenter/base.rb in active_presenter-1.2.1

- old
+ new

@@ -34,18 +34,41 @@ presented[t] = types_and_classes[t] end end - def self.human_attribute_name(attribute_name) + def self.human_attribute_name(attribute_key_name, options = {}) presentable_type = presented.keys.detect do |type| - attribute_name.to_s.starts_with?("#{type}_") + attribute_key_name.to_s.starts_with?("#{type}_") || attribute_key_name.to_s == type.to_s end + attribute_key_name_without_class = attribute_key_name.to_s.gsub("#{presentable_type}_", "") - attribute_name.to_s.gsub("#{presentable_type}_", "").humanize + if presented[presentable_type] and attribute_key_name_without_class != presentable_type.to_s + presented[presentable_type].human_attribute_name(attribute_key_name_without_class, options) + else + I18n.translate(presentable_type, options.merge(:default => presentable_type.to_s.humanize, :scope => [:activerecord, :models])) + end end + # Since ActivePresenter does not descend from ActiveRecord, we need to + # mimic some ActiveRecord behavior in order for the ActiveRecord::Errors + # object we're using to work properly. + # + # This problem was introduced with Rails 2.3.4. + # Fix courtesy http://gist.github.com/191263 + def self.self_and_descendants_from_active_record # :nodoc: + [self] + end + + def self.human_name(options = {}) # :nodoc: + defaults = self_and_descendants_from_active_record.map do |klass| + :"#{klass.name.underscore}" + end + defaults << self.name.humanize + I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options)) + end + # Accepts arguments in two forms. For example, if you had a SignupPresenter that presented User, and Account, you could specify arguments in the following two forms: # # 1. SignupPresenter.new(:user_login => 'james', :user_password => 'swordfish', :user_password_confirmation => 'swordfish', :account_subdomain => 'giraffesoft') # - This form is useful for initializing a new presenter from the params hash: i.e. SignupPresenter.new(params[:signup_presenter]) # 2. SignupPresenter.new(:user => User.find(1), :account => Account.find(2)) @@ -58,21 +81,24 @@ # def initialize(args = {}) args ||= {} presented.each do |type, klass| - send("#{type}=", args[type].is_a?(klass) ? args.delete(type) : klass.new) + value = args.delete(type) + send("#{type}=", value.is_a?(klass) ? value : klass.new) end self.attributes = args end # Set the attributes of the presentable instances using # the type_attribute form (i.e. user_login => 'james'), or # the multiparameter attribute form (i.e. {user_birthday(1i) => "1980", user_birthday(2i) => "3"}) # def attributes=(attrs) + return if attrs.nil? + multi_parameter_attributes = {} attrs.each do |k,v| if (base_attribute = k.to_s.split("(").first) != k.to_s presentable = presentable_for(base_attribute) @@ -88,11 +114,11 @@ end 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) + def respond_to?(method, include_private = false) presented_attribute?(method) || super end # Handles the decision about whether to delegate getters and setters to presentable instances. # @@ -220,10 +246,14 @@ "#{type}_" end def merge_errors(presented_inst, type) presented_inst.errors.each do |att,msg| - errors.add(attribute_prefix(type)+att, msg) + if att == 'base' + errors.add(type, msg) + else + errors.add(attribute_prefix(type)+att, msg) + end end end def attribute_protected?(name) presentable = presentable_for(name)