Skip to content

Avoid mutating the input - #258

Open
semenyukdmitry wants to merge 1 commit into
madeintandem:masterfrom
semenyukdmitry:no-mutate
Open

semenyukdmitry wants to merge 1 commit into
madeintandem:masterfrom
semenyukdmitry:no-mutate

Conversation

@semenyukdmitry

Copy link
Copy Markdown

After the 1.4.2 update, passing a constant to jsonb_accessor started
mutating it, which might be really confusing to the user.

SCHEMA = {
  tags:  [:string, { array: true, default: [] }],
  title: [:string, { store_key: :t }]
}.freeze

class Widget < ActiveRecord::Base
  jsonb_accessor :data, SCHEMA
end

SCHEMA[:tags]   # => [:string] <- the options hash is gone
SCHEMA[:title]  # => [:string]

The PR fixes it by duplicating it before use. In addition to that, passing
a frozen a constant would now work fine.

After the 1.4.2 update, passing a constant to `jsonb_accessor` started
mutating it, which might be really confusing to the user.

```rb
SCHEMA = {
  tags:  [:string, { array: true, default: [] }],
  title: [:string, { store_key: :t }]
}.freeze

class Widget < ActiveRecord::Base
  jsonb_accessor :data, SCHEMA
end

SCHEMA[:tags]   # => [:string] <- the options hash is gone
SCHEMA[:title]  # => [:string]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant