如何为属性添加别名

时间:2014-07-09 11:48:36

标签: ruby-on-rails ruby spree rabl

我想为属性:phone添加别名(在@@address_attribute下),比如:phone_no。我该怎么做?

module Spree
  module Api
    module ApiHelpers
      ATTRIBUTES = [
        :product_attributes,
        :product_property_attributes,
        :variant_attributes,
        :image_attributes,
        :option_value_attributes,
        :order_attributes,
        :line_item_attributes,
        :option_type_attributes,
        :payment_attributes,
        :payment_method_attributes,
        :shipment_attributes,
        :taxonomy_attributes,
        :taxon_attributes,
        :inventory_unit_attributes,
        :return_authorization_attributes,
        :address_attributes,
        :country_attributes,
        :state_attributes,
        :adjustment_attributes,
        :inventory_unit_attributes,
        :return_authorization_attributes,
        :creditcard_attributes,
        :payment_source_attributes,
        :user_attributes,
        :property_attributes,
        :stock_location_attributes,
        :stock_movement_attributes,
        :stock_item_attributes
      ]
      mattr_reader *ATTRIBUTES
      def required_fields_for(model)
        required_fields = model._validators.select do |field, validations|
          validations.any? { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
        end.map(&:first) # get fields that are invalid
        # Permalinks presence is validated, but are really automatically generated
        # Therefore we shouldn't tell API clients that they MUST send one through
        required_fields.map!(&:to_s).delete("permalink")
        # Do not require slugs, either
        required_fields.delete("slug")
        required_fields
      end

      @@address_attributes = [
        :id, :firstname, :lastname, :full_name, :address1, :address2, :city,
        :zipcode, :phone, :company, :alternative_phone, :country_id, :state_id,
        :state_name, :state_text
      ]

    end
  end
end

2 个答案:

答案 0 :(得分:0)

怎么样

def self.phone_attribute
  :phone
end

@@address_attributes = [
  :id, :firstname, :lastname, :full_name, :address1, :address2, :city,
  :zipcode, phone_attribute, :company, :alternative_phone, :country_id, :state_id,
  :state_name, :state_text
]

但是,你确定你应该在这里使用类变量吗?我建议这样做更好:

def self.phone_attribute
  :phone
end

def self.address_attributes
  @address_attributes ||= [
    :id, :firstname, :lastname, :full_name, :address1, :address2, :city,
    :zipcode, phone_attribute, :company, :alternative_phone, :country_id, :state_id,
    :state_name, :state_text
  ]
end

使用模块实例变量并通过模块方法公开它。

答案 1 :(得分:0)

alias_attribute无法为您效力,因为:phone不是一个合适的属性。它只是address_attributes哈希上的一个键,所以你真正要求做的就是别名那个键。你为什么要这样做?

如果您希望它工作,您需要将:phone定义为真正的模块属性。也许是这样

module Spree
  module Api
    module ApiHelpers
      # ...
      module AddressAttributes
        attributes = [
          :id, :firstname, :lastname, :full_name, :address1, :address2, :city,
          :zipcode, :phone, :company, :alternative_phone, :country_id, :state_id,
          :state_name, :state_text
        ]

        mattr_accessor *attributes
        class << self
          alias_attribute :phone_no, :phone
        end
      end
      @@address_attributes = AddressAttributes
    end
  end
end

Spree::Api::ApiHelpers.address_attributes.phone_no #=> same as :phone

编辑:需要在单例类上执行别名,因为它们是类/模块属性