her 0.8.2 → 0.10.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.rubocop.yml +1291 -0
- data/.travis.yml +6 -1
- data/README.md +29 -11
- data/her.gemspec +3 -5
- data/lib/her/api.rb +16 -9
- data/lib/her/middleware/json_api_parser.rb +1 -1
- data/lib/her/model/associations/association.rb +32 -5
- data/lib/her/model/associations/association_proxy.rb +1 -1
- data/lib/her/model/associations/belongs_to_association.rb +1 -1
- data/lib/her/model/associations/has_many_association.rb +3 -3
- data/lib/her/model/attributes.rb +105 -75
- data/lib/her/model/http.rb +3 -3
- data/lib/her/model/introspection.rb +1 -1
- data/lib/her/model/orm.rb +96 -19
- data/lib/her/model/parse.rb +27 -17
- data/lib/her/model/relation.rb +46 -2
- data/lib/her/version.rb +1 -1
- data/spec/api_spec.rb +34 -31
- data/spec/collection_spec.rb +25 -10
- data/spec/json_api/model_spec.rb +75 -72
- data/spec/middleware/accept_json_spec.rb +1 -1
- data/spec/middleware/first_level_parse_json_spec.rb +20 -20
- data/spec/middleware/json_api_parser_spec.rb +26 -7
- data/spec/middleware/second_level_parse_json_spec.rb +8 -9
- data/spec/model/associations/association_proxy_spec.rb +2 -5
- data/spec/model/associations_spec.rb +617 -295
- data/spec/model/attributes_spec.rb +114 -107
- data/spec/model/callbacks_spec.rb +59 -27
- data/spec/model/dirty_spec.rb +70 -29
- data/spec/model/http_spec.rb +67 -35
- data/spec/model/introspection_spec.rb +26 -22
- data/spec/model/nested_attributes_spec.rb +31 -31
- data/spec/model/orm_spec.rb +332 -157
- data/spec/model/parse_spec.rb +250 -77
- data/spec/model/paths_spec.rb +109 -109
- data/spec/model/relation_spec.rb +89 -69
- data/spec/model/validations_spec.rb +6 -6
- data/spec/model_spec.rb +17 -17
- data/spec/spec_helper.rb +2 -3
- data/spec/support/macros/model_macros.rb +2 -2
- metadata +36 -63
@@ -4,40 +4,40 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
4
4
|
describe Her::Model::NestedAttributes do
|
5
5
|
context "with a belongs_to association" do
|
6
6
|
before do
|
7
|
-
Her::API.setup :
|
7
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
8
8
|
builder.use Her::Middleware::FirstLevelParseJSON
|
9
9
|
builder.use Faraday::Request::UrlEncoded
|
10
10
|
end
|
11
11
|
|
12
12
|
spawn_model "Foo::User" do
|
13
|
-
belongs_to :company, :
|
13
|
+
belongs_to :company, path: "/organizations/:id", foreign_key: :organization_id
|
14
14
|
accepts_nested_attributes_for :company
|
15
15
|
end
|
16
16
|
|
17
17
|
spawn_model "Foo::Company"
|
18
18
|
|
19
|
-
@user_with_data_through_nested_attributes = Foo::User.new :
|
19
|
+
@user_with_data_through_nested_attributes = Foo::User.new name: "Test", company_attributes: { name: "Example Company" }
|
20
20
|
end
|
21
21
|
|
22
22
|
context "when child does not yet exist" do
|
23
23
|
it "creates an instance of the associated class" do
|
24
|
-
@user_with_data_through_nested_attributes.company.
|
25
|
-
@user_with_data_through_nested_attributes.company.name.
|
24
|
+
expect(@user_with_data_through_nested_attributes.company).to be_a(Foo::Company)
|
25
|
+
expect(@user_with_data_through_nested_attributes.company.name).to eq("Example Company")
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
context "when child does exist" do
|
30
30
|
it "updates the attributes of the associated object" do
|
31
|
-
@user_with_data_through_nested_attributes.company_attributes = { :
|
32
|
-
@user_with_data_through_nested_attributes.company.
|
33
|
-
@user_with_data_through_nested_attributes.company.name.
|
31
|
+
@user_with_data_through_nested_attributes.company_attributes = { name: "Fünke's Company" }
|
32
|
+
expect(@user_with_data_through_nested_attributes.company).to be_a(Foo::Company)
|
33
|
+
expect(@user_with_data_through_nested_attributes.company.name).to eq("Fünke's Company")
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
context "with a has_one association" do
|
39
39
|
before do
|
40
|
-
Her::API.setup :
|
40
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
41
41
|
builder.use Her::Middleware::FirstLevelParseJSON
|
42
42
|
builder.use Faraday::Request::UrlEncoded
|
43
43
|
end
|
@@ -49,28 +49,28 @@ describe Her::Model::NestedAttributes do
|
|
49
49
|
|
50
50
|
spawn_model "Foo::Pet"
|
51
51
|
|
52
|
-
@user_with_data_through_nested_attributes = Foo::User.new :
|
52
|
+
@user_with_data_through_nested_attributes = Foo::User.new name: "Test", pet_attributes: { name: "Hasi" }
|
53
53
|
end
|
54
54
|
|
55
55
|
context "when child does not yet exist" do
|
56
56
|
it "creates an instance of the associated class" do
|
57
|
-
@user_with_data_through_nested_attributes.pet.
|
58
|
-
@user_with_data_through_nested_attributes.pet.name.
|
57
|
+
expect(@user_with_data_through_nested_attributes.pet).to be_a(Foo::Pet)
|
58
|
+
expect(@user_with_data_through_nested_attributes.pet.name).to eq("Hasi")
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
context "when child does exist" do
|
63
63
|
it "updates the attributes of the associated object" do
|
64
|
-
@user_with_data_through_nested_attributes.pet_attributes = { :
|
65
|
-
@user_with_data_through_nested_attributes.pet.
|
66
|
-
@user_with_data_through_nested_attributes.pet.name.
|
64
|
+
@user_with_data_through_nested_attributes.pet_attributes = { name: "Rodriguez" }
|
65
|
+
expect(@user_with_data_through_nested_attributes.pet).to be_a(Foo::Pet)
|
66
|
+
expect(@user_with_data_through_nested_attributes.pet.name).to eq("Rodriguez")
|
67
67
|
end
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
71
|
context "with a has_many association" do
|
72
72
|
before do
|
73
|
-
Her::API.setup :
|
73
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
74
74
|
builder.use Her::Middleware::FirstLevelParseJSON
|
75
75
|
builder.use Faraday::Request::UrlEncoded
|
76
76
|
end
|
@@ -82,23 +82,23 @@ describe Her::Model::NestedAttributes do
|
|
82
82
|
|
83
83
|
spawn_model "Foo::Pet"
|
84
84
|
|
85
|
-
@user_with_data_through_nested_attributes = Foo::User.new :
|
85
|
+
@user_with_data_through_nested_attributes = Foo::User.new name: "Test", pets_attributes: [{ name: "Hasi" }, { name: "Rodriguez" }]
|
86
86
|
end
|
87
87
|
|
88
88
|
context "when children do not yet exist" do
|
89
89
|
it "creates an instance of the associated class" do
|
90
|
-
@user_with_data_through_nested_attributes.pets.length.
|
91
|
-
@user_with_data_through_nested_attributes.pets[0].
|
92
|
-
@user_with_data_through_nested_attributes.pets[1].
|
93
|
-
@user_with_data_through_nested_attributes.pets[0].name.
|
94
|
-
@user_with_data_through_nested_attributes.pets[1].name.
|
90
|
+
expect(@user_with_data_through_nested_attributes.pets.length).to eq(2)
|
91
|
+
expect(@user_with_data_through_nested_attributes.pets[0]).to be_a(Foo::Pet)
|
92
|
+
expect(@user_with_data_through_nested_attributes.pets[1]).to be_a(Foo::Pet)
|
93
|
+
expect(@user_with_data_through_nested_attributes.pets[0].name).to eq("Hasi")
|
94
|
+
expect(@user_with_data_through_nested_attributes.pets[1].name).to eq("Rodriguez")
|
95
95
|
end
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
99
|
context "with a has_many association as a Hash" do
|
100
100
|
before do
|
101
|
-
Her::API.setup :
|
101
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
102
102
|
builder.use Her::Middleware::FirstLevelParseJSON
|
103
103
|
builder.use Faraday::Request::UrlEncoded
|
104
104
|
end
|
@@ -110,25 +110,25 @@ describe Her::Model::NestedAttributes do
|
|
110
110
|
|
111
111
|
spawn_model "Foo::Pet"
|
112
112
|
|
113
|
-
@user_with_data_through_nested_attributes_as_hash = Foo::User.new :
|
113
|
+
@user_with_data_through_nested_attributes_as_hash = Foo::User.new name: "Test", pets_attributes: { "0" => { name: "Hasi" }, "1" => { name: "Rodriguez" } }
|
114
114
|
end
|
115
115
|
|
116
116
|
context "when children do not yet exist" do
|
117
117
|
it "creates an instance of the associated class" do
|
118
|
-
@user_with_data_through_nested_attributes_as_hash.pets.length.
|
119
|
-
@user_with_data_through_nested_attributes_as_hash.pets[0].
|
120
|
-
@user_with_data_through_nested_attributes_as_hash.pets[1].
|
121
|
-
@user_with_data_through_nested_attributes_as_hash.pets[0].name.
|
122
|
-
@user_with_data_through_nested_attributes_as_hash.pets[1].name.
|
118
|
+
expect(@user_with_data_through_nested_attributes_as_hash.pets.length).to eq(2)
|
119
|
+
expect(@user_with_data_through_nested_attributes_as_hash.pets[0]).to be_a(Foo::Pet)
|
120
|
+
expect(@user_with_data_through_nested_attributes_as_hash.pets[1]).to be_a(Foo::Pet)
|
121
|
+
expect(@user_with_data_through_nested_attributes_as_hash.pets[0].name).to eq("Hasi")
|
122
|
+
expect(@user_with_data_through_nested_attributes_as_hash.pets[1].name).to eq("Rodriguez")
|
123
123
|
end
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
127
|
context "with an unknown association" do
|
128
128
|
it "raises an error" do
|
129
|
-
expect
|
129
|
+
expect do
|
130
130
|
spawn_model("Foo::User") { accepts_nested_attributes_for :company }
|
131
|
-
|
131
|
+
end.to raise_error(Her::Errors::AssociationUnknownError, "Unknown association name :company")
|
132
132
|
end
|
133
133
|
end
|
134
134
|
end
|