active-hash / active_hash

A readonly ActiveRecord-esque base class that lets you use a hash, a Yaml file or a custom file as the datasource
MIT License
1.21k stars 178 forks source link

Support StrongParameters #201

Closed rhiroe closed 9 months ago

rhiroe commented 4 years ago

When an allowed ActionController::Parameters object was given as an argument, an empty array was returned. I wanted to get the data even with the parameters. Even if the argument is ActionController::Parameters object, records will be searched correctly.

# in rails
class Country < ActiveHash::Base
  self.data = [
    {:id => 1, :name => "US"},
    {:id => 2, :name => "Canada"}
  ]
 end
Country.where(name: 'US').to_a
#=> [#<Country:0x0000... @attributes={:id=>1, :name=>"US"}>]

params = ActionController::Parameters.new(name: 'US')

# before
Country.where(params.permit!).to_a
#=> []

# after
Country.where(params.permit!).to_a
#=> [#<Country:0x0000... @attributes={:id=>1, :name=>"US"}>]

Country.where(params).to_a
#=> ActiveHash::ForbiddenAttributesError

Tell me if insufficient testing.

And ActiveHash::Relation#where also raise an ArgumentError when an unexpected object is passed as an argument.

# before
Country.where([:id, 'US'])
#=> NoMethodError (undefined method `keys' for [:name, "US"]:Array)

# after
Country.where([:id, 'US'])
#=> ArgumentError (Unsupported argument type: [:name, "US"] (Array))

Thanks.