From d979491911dfcccf60300569269531f3672a071f Mon Sep 17 00:00:00 2001 From: Pavel Kalashnikov Date: Mon, 19 Feb 2024 05:55:42 +0400 Subject: [PATCH] Issue #48. Add tests about apply_to_nil --- lib/tramway/base_form.rb | 16 +++++++++++----- lib/tramway/forms/normalizations.rb | 1 + spec/dummy/app/forms/admin_form.rb | 2 +- spec/dummy/app/forms/user_form.rb | 2 +- .../db/migrate/20230627061428_create_users.rb | 8 ++++---- spec/dummy/db/schema.rb | 8 ++++---- spec/forms/normalizes_form_spec.rb | 17 +++++++++++++++++ 7 files changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/tramway/base_form.rb b/lib/tramway/base_form.rb index 53a30e94..a8b6d2bb 100644 --- a/lib/tramway/base_form.rb +++ b/lib/tramway/base_form.rb @@ -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 diff --git a/lib/tramway/forms/normalizations.rb b/lib/tramway/forms/normalizations.rb index ebf10ef2..feb227ac 100644 --- a/lib/tramway/forms/normalizations.rb +++ b/lib/tramway/forms/normalizations.rb @@ -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: }) diff --git a/spec/dummy/app/forms/admin_form.rb b/spec/dummy/app/forms/admin_form.rb index 1162ced4..9d22ebe0 100644 --- a/spec/dummy/app/forms/admin_form.rb +++ b/spec/dummy/app/forms/admin_form.rb @@ -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 diff --git a/spec/dummy/app/forms/user_form.rb b/spec/dummy/app/forms/user_form.rb index 48083acb..034337a6 100644 --- a/spec/dummy/app/forms/user_form.rb +++ b/spec/dummy/app/forms/user_form.rb @@ -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 diff --git a/spec/dummy/db/migrate/20230627061428_create_users.rb b/spec/dummy/db/migrate/20230627061428_create_users.rb index 36418d76..583389f8 100644 --- a/spec/dummy/db/migrate/20230627061428_create_users.rb +++ b/spec/dummy/db/migrate/20230627061428_create_users.rb @@ -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 diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 3fe14c0f..e043e206 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -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' diff --git a/spec/forms/normalizes_form_spec.rb b/spec/forms/normalizes_form_spec.rb index 5388c2e1..993a1b5c 100644 --- a/spec/forms/normalizes_form_spec.rb +++ b/spec/forms/normalizes_form_spec.rb @@ -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