Skip to content

Commit

Permalink
Issue #48. Add tests about apply_to_nil
Browse files Browse the repository at this point in the history
  • Loading branch information
kalashnikovisme committed Feb 19, 2024
1 parent 2cba8a1 commit d979491
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
16 changes: 11 additions & 5 deletions lib/tramway/base_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,22 @@ def respond_to_missing?(method_name, include_private = false)
private

def __submit(params)
values = params
__apply_properties __apply_normalizations params
end

self.class.normalizations.each do |attribute, normalization|
if params.key?(attribute) || normalization[:apply_to_nil]
values[attribute] = instance_exec(params[attribute], &normalization[:proc])
def __apply_normalizations(params)
self.class.normalizations.reduce(params) do |hash, (attribute, normalization)|
if hash.key?(attribute) || normalization[:apply_to_nil]
hash.merge(attribute => instance_exec(hash[attribute], &normalization[:proc]))
else
hash
end
end
end

def __apply_properties(params)
self.class.properties.each do |attribute|
public_send("#{attribute}=", values[attribute]) if values.key?(attribute)
public_send("#{attribute}=", params[attribute]) if params.key?(attribute)
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/tramway/forms/normalizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Forms
# This is the same `normalizes` feature like in Rails
# https://api.rubyonrails.org/v7.1/classes/ActiveRecord/Normalization/ClassMethods.html#method-i-normalizes
module Normalizations
# :reek:BooleanParameter { enabled: false }
def normalizes(*attributes, with:, apply_to_nil: false)
attributes.each do |attribute|
@normalizations.merge!(attribute => { proc: with, apply_to_nil: })
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/forms/admin_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class AdminForm < UserForm
properties :permissions, :first_name, :last_name

normalizes :permissions, with: ->(value) { value.is_a?(Array) ? value : value.split(',') }
normalizes :first_name, :last_name, with: ->(value) { value.strip }
normalizes :first_name, :last_name, with: ->(value) { value&.strip }
end
2 changes: 1 addition & 1 deletion spec/dummy/app/forms/user_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
class UserForm < Tramway::BaseForm
properties :email, :role

normalizes :email, with: ->(value) { value.strip.downcase }
normalizes :email, with: ->(value) { value&.strip&.downcase }
end
8 changes: 4 additions & 4 deletions spec/dummy/db/migrate/20230627061428_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :email, null: false, default: ''
t.string :first_name, null: false, default: ''
t.string :last_name, null: false, default: ''
t.string :encrypted_password, null: false, default: ''
t.string :email
t.string :first_name
t.string :last_name
t.string :encrypted_password

t.string :reset_password_token
t.datetime :reset_password_sent_at
Expand Down
8 changes: 4 additions & 4 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
end

create_table 'users', force: :cascade do |t|
t.string 'email', default: '', null: false
t.string 'first_name', default: '', null: false
t.string 'last_name', default: '', null: false
t.string 'encrypted_password', default: '', null: false
t.string 'email'
t.string 'first_name'
t.string 'last_name'
t.string 'encrypted_password'
t.string 'reset_password_token'
t.datetime 'reset_password_sent_at'
t.datetime 'remember_created_at'
Expand Down
17 changes: 17 additions & 0 deletions spec/forms/normalizes_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@
expect(user.last_name).to eq 'Selezneva'
end
end

context 'when normalizing attributes with apply_on_nil: true' do
it 'applies normalization to nil values' do
AdminForm.new(user).submit(first_name: nil, last_name: nil)

expect(user.first_name).to be_nil
expect(user.last_name).to be_nil
end
end

context 'when normalizing attributes without apply_on_nil' do
it 'does not apply normalization to nil values' do
UserForm.new(user).submit(email: nil)

expect(user.email).to eq nil
end
end
end

context 'inheritance and extension' do
Expand Down

0 comments on commit d979491

Please sign in to comment.