Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a Form to mimic a model with an attribute with the same name #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Layout/IndentationWidth:
Width: 2

Metrics/ClassLength:
Max: 110
Max: 115
Exclude:
- 'spec/**/*_spec.rb'

Expand Down
12 changes: 10 additions & 2 deletions lib/rectify/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Form

def self.from_params(params, additional_params = {})
params_hash = hash_from(params)
mimicked_params = ensure_hash(params_hash[mimicked_model_name])

attributes_hash = params_hash
.fetch(mimicked_model_name, {})
.merge(params_hash)
.merge(mimicked_params)
.merge(additional_params)

formatted_attributes = FormatAttributesHash
Expand Down Expand Up @@ -54,6 +54,14 @@ def self.hash_from(params)
params.with_indifferent_access
end

def self.ensure_hash(object)
if object.is_a?(Hash)
object
else
{}
end
end

def persisted?
id.present? && id.to_i > 0
end
Expand Down
5 changes: 5 additions & 0 deletions spec/db/migrate/20180531090029_add_user_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserToUser < ActiveRecord::Migration[5.2]
def change
add_column :users, :user, :string
end
end
3 changes: 2 additions & 1 deletion spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2016_05_25_115421) do
ActiveRecord::Schema.define(version: 2018_05_31_090029) do

create_table "addresses", force: :cascade do |t|
t.string "street", default: "", null: false
Expand Down Expand Up @@ -38,6 +38,7 @@
t.boolean "active", default: true, null: false
t.integer "address_id"
t.datetime "last_logged_in"
t.string "user"
end

end
1 change: 1 addition & 0 deletions spec/fixtures/forms/user_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class UserForm < Rectify::Form
mimic :user

attribute :user, String
attribute :first_name, String
attribute :age, Integer
attribute :colours, Array
Expand Down
17 changes: 15 additions & 2 deletions spec/lib/rectify/form_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
RSpec.describe Rectify::Form do
describe ".new" do
it "populates attributes from a string key hash" do
form = UserForm.new("first_name" => "Andy", "age" => 38)
form = UserForm.new(
"user" => "andy38",
"first_name" => "Andy",
"age" => 38
)

expect(form).to have_attributes(
:user => "andy38",
:first_name => "Andy",
:age => 38
)
end

it "populates attributes from a symbol key hash" do
form = UserForm.new(:first_name => "Andy", :age => 38)
form = UserForm.new(:user => "andy38", :first_name => "Andy", :age => 38)

expect(form).to have_attributes(
:user => "andy38",
:first_name => "Andy",
:age => 38
)
Expand All @@ -25,6 +31,7 @@
"id" => "1",
"other_id" => "2",
"user" => {
"user" => "andy38",
"first_name" => "Andy",
"age" => "38",
"colours" => %w[red blue green],
Expand All @@ -48,6 +55,7 @@
form = UserForm.from_params(params)

expect(form).to have_attributes(
:user => "andy38",
:first_name => "Andy",
:age => 38,
:colours => %w[red blue green]
Expand Down Expand Up @@ -131,6 +139,7 @@
form = ChildForm.from_params(params)

expect(form).to have_attributes(
:user => "andy38",
:first_name => "Andy",
:age => 38,
:school => "Rutlish"
Expand Down Expand Up @@ -161,6 +170,7 @@
describe ".from_model" do
let(:model) do
User.new(
:user => "andy38",
:first_name => "Andy",
:age => 38,
:contacts => [
Expand All @@ -180,6 +190,7 @@
form = UserForm.from_model(model)

expect(form).to have_attributes(
:user => "andy38",
:first_name => "Andy",
:age => 38
)
Expand Down Expand Up @@ -217,6 +228,7 @@
it "populates attributes from a json string" do
json = <<-JSON
{
"user": "andy38",
"first_name": "Andy",
"age": 38,
"address": {
Expand All @@ -231,6 +243,7 @@
form = UserForm.from_json(json)

expect(form).to have_attributes(
:user => "andy38",
:first_name => "Andy",
:age => 38
)
Expand Down