Module: AttributeExt::HiddenAttributes::ClassMethods

Defined in:
lib/attribute_ext/hidden_attributes.rb

Instance Method Summary collapse

Instance Method Details

#hide_attributes(*attrs) ⇒ Object

Adds attribute to a blacklist that will be hidden when serializing if optional conditions are true.

class User < ActiveRecord::Base
  hide_attributes :password               # always hide
  hide_attributes :email, :if => Proc.new { |user| user.hide_email? }
  hide_attributes :not_in_json, :only => :json
  hide_attributes :except_xml_hash, :except => [:xml, :hash]
end

All given conditions to a rule must be true if attributes should be hidden. Attributes can appear in more than one rule.

Options:

:if

Requires a Proc block to be true.

:unless

Requires a Proc block to be false.

:only

Requires export format to be in given array. A non array object will be converted in to an array only containing given object.

:except

Requires export format to not be in given array. A non array object will be converted in to an array only containing given object.

:on_hash

By default rules will not be applied when serializing to hash when no :only or :except rule is specified. If :on_hash is true rule will also apply to hash serialization. If an :only or :except option is given :on_hash does nothing.



44
45
46
47
48
49
50
51
52
# File 'lib/attribute_ext/hidden_attributes.rb', line 44

def hide_attributes(*attrs)
  @hidden_attributes ||= []
  if attrs.empty?
    @hidden_attributes
  else
    options = attrs.last.is_a?(Hash) ? attrs.pop : {}
    @hidden_attributes << [attrs, hide_attributes_opts(options)]
  end
end