module StrongParametedAddOn::ClassMethods

Public Instance Methods

strong_parameted(attrs = nil) click to toggle source

Strong parameted

# 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