Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/cancan_strong_parameters/ruby/hash.rb,
lib/cancan_strong_parameters/controller.rb

Instance Method Summary collapse

Instance Method Details

#attributizedObject

Converts keys with hash values – e.g. posts: {} – to posts_attributes for nested forms.

Also, Allows rails specific values like _destroy or _delete.

NOTE: You must enable ‘allow_destroy: true` in your call to `accepts_nested_attributes_for` anyway, so this is secure to whitelist here.



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cancan_strong_parameters/controller.rb', line 138

def attributized
  defaults = CancanStrongParameters::Controller::HASH_DEFAULTS
  
  Hash.new.tap do |h|
    self.each do |k,v|
      if v.respond_to? :attributized
        h[:"#{k}_attributes"] = v.attributized + defaults
      else
        h[k] = v == Array ? [] : v
      end
    end
  end
end

#indifferentObject Also known as: indifferent_access



4
5
6
# File 'lib/cancan_strong_parameters/ruby/hash.rb', line 4

def indifferent
  ActiveSupport::HashWithIndifferentAccess.new(self)
end

#to_parameter_arrayObject

Converts keyed nested_forms (like task_attributes: => {}) to normal params arrays.



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cancan_strong_parameters/controller.rb', line 153

def to_parameter_array
  return self if self.empty?
  
  return self unless (k = self.keys.first).is_a?(String) and k[0..3] == "new_" or k.is_i? or k.is_hex?
  
  Array.new.tap do |a|
    self.each do |k,v|
      a << v.standardized
    end
  end
end