2014-07-09 6 views
0

: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 
+0

가능한 중복 클래스 속성 때문에 싱글 톤 클래스에 별칭을 할 필요가 .com/questions/6740379/ruby-alias-a-method-from-class) – Upperstage

+0

-1 이렇게 긴 목록을 게시하십시오. 귀하의 요점을 보여주기 위해 그러한 긴 목록을 작성할 필요는 없습니다. – sawa

답변

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 

모듈 인스턴스 변수를 사용하고 그것을 모듈 방법으로 노출합니다.

+0

[링크] (http://apidock.com/rails/Module/alias_attribute). alias_attribute를 사용하고 싶습니다. – padawan

+0

결과 json 파일에서 실제로 이름이 변경되기를 원합니다. @ 레기 B – padawan

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 

과 같이 편집 : [: 별칭 클래스의 메소드 루비] (HTTP : // 유래가/모듈의

관련 문제