Module: StrongParametedAddOn::ClassMethods

Defined in:
lib/spyro/controllers/strong_parameted.rb

Instance Method Summary collapse

Instance Method Details

#strong_parameted(attrs = nil) ⇒ Object

Strong parameted



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/spyro/controllers/strong_parameted.rb', line 8

def strong_parameted attrs = nil
  cattr_accessor :strong_attributes

  model = self.name.to_s[0..-11].split('::').last.singularize
  model_sym = model.underscore.to_sym
  if attrs
    attrs.map! {|elem| (elem == :all ? (model.constantize.column_names - ['id', 'updated_at', 'created_at']).map!(&:to_sym) : elem) }
  else
    attrs = (model.constantize.column_names - ['id', 'updated_at', 'created_at']).map!(&:to_sym)
  end

  self.strong_attributes = attrs

  define_method :resource_params do
    return [] if request.get?
    model_hash = params.require(model_sym)
    return (model_hash.respond_to?(:permit) ? [model_hash.permit(attrs)] : [])
  end
end