Module: AttributeExt::HiddenAttributes

Defined in:
lib/attribute_ext/hidden_attributes.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



3
4
5
6
7
8
# File 'lib/attribute_ext/hidden_attributes.rb', line 3

def self.included(base) # :nodoc:
  base.extend(ClassMethods)
  base.alias_method_chain :to_xml, :hidden_attrs
  base.alias_method_chain :as_json, :hidden_attrs
  base.alias_method_chain :serializable_hash, :hidden_attrs
end

Instance Method Details

#as_json_with_hidden_attrs(options = nil, &block) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
# File 'lib/attribute_ext/hidden_attributes.rb', line 78

def as_json_with_hidden_attrs(options = nil, &block) # :nodoc:
  options ||= {}
  options[:except] = hidden_attribute_names(:json, options)
  options[:hidden_attributes_format] = :json
        
  as_json_without_hidden_attrs(options)
end

#hidden_attribute_names(format, options = {}) ⇒ Object

Returns an array with attributes to hide from serialization.

This method should only be used to test own rules without need to run a formatter and validate the generated output. See AttributeExt specs for details.

hidden_attribute_names :format, :options => :hash


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/attribute_ext/hidden_attributes.rb', line 100

def hidden_attribute_names(format, options = {})
  if options[:except].is_a?(Array)
    names = options[:except]
  else
    names = []
    names << options[:except] if options[:except]
  end
  
  self.class.hide_attributes.collect do |attrs, opts|
    next unless opts[:only].empty? or opts[:only].include?(format)
    next unless opts[:except].empty? or !opts[:except].include?(format)
    next unless opts[:if].nil? or hidden_attr_call_block(opts[:if], format, options)
    next unless opts[:unless].nil? or !hidden_attr_call_block(opts[:unless], format, options)
    
    names += attrs.collect(&:to_s)
  end
  names.uniq
end

#serializable_hash_with_hidden_attrs(options = nil) ⇒ Object

:nodoc:



86
87
88
89
90
91
# File 'lib/attribute_ext/hidden_attributes.rb', line 86

def serializable_hash_with_hidden_attrs(options = nil) # :nodoc:
  options ||= {}
  options[:except] = hidden_attribute_names((options[:hidden_attributes_format] || :hash), options)

  serializable_hash_without_hidden_attrs(options)
end

#to_xml_with_hidden_attrs(options = nil, &block) ⇒ Object

:nodoc:



70
71
72
73
74
75
76
# File 'lib/attribute_ext/hidden_attributes.rb', line 70

def to_xml_with_hidden_attrs(options = nil, &block) # :nodoc:
  options ||= {}
  options[:except] = hidden_attribute_names(:xml, options)
  options[:hidden_attributes_format] = :xml
  
  to_xml_without_hidden_attrs(options)
end