her 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e484ba1a9e290f7fd1d8f64feed7b6ceb0a4e6b0
4
- data.tar.gz: fc62b8ba7f6de6a75fe121debb564fffd7e0773d
3
+ metadata.gz: c0f87d36e43920519c831bffab58456dd03219dd
4
+ data.tar.gz: 8f347722b32acb8f9a7f8f372683b09e9c305d8f
5
5
  SHA512:
6
- metadata.gz: 87b66eb01fd1376323334646ce77d8ea114ab5d2947215a7b5bc66a3bbcd895ec38df0bce59d28bbd92e11b818394dc52136607f141024dc20263edf6a42a123
7
- data.tar.gz: 79629aa61a794ff563838665953051fb03a07784820a87fc649b9247379461cb537df9b5cd2c8ab4615df63c0029f7df8a8bf7f3f2fe24531aa231c7fd08a187
6
+ metadata.gz: 4a239ac856dd17577b9adcfbf96d454a20ec12cdfa605bd45625261aeb93a31b04d95712b2ed5c7901f48371eadbfb08076a4318cc85276373e3e0dfb6de31a8
7
+ data.tar.gz: 9a9b886cc0050fecb2d3bff8fcec90dddd3ffac98c1af8c61c177ef5314036f19939ab29542a52305c427ebd6f8b78100199d2fddf66a4031299cbb800dff9fa
@@ -0,0 +1 @@
1
+ 2.2.4
@@ -38,6 +38,7 @@ matrix:
38
38
  rvm: 2.1.6
39
39
 
40
40
  before_install:
41
- - gem install bundler
41
+ - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
42
+ - gem install bundler -v '< 2'
42
43
 
43
44
  script: "echo 'COME ON!' && bundle exec rake spec"
data/README.md CHANGED
@@ -91,6 +91,10 @@ user.fullname = "Lindsay Fünke" # OR user.assign_attributes(fullname: "Lindsay
91
91
  user.save # returns false if it fails, errors in user.response_errors array
92
92
  # PUT "/users/1" with `fullname=Lindsay+Fünke`
93
93
 
94
+ user.update_attributes(fullname: "Maeby Fünke")
95
+ # PUT "/users/1" with `fullname=Maeby+Fünke`
96
+
97
+ # => PUT /users/1 { "id": 1, "name": "new new name" }
94
98
  # Update a resource without fetching it
95
99
  User.save_existing(1, fullname: "Lindsay Fünke")
96
100
  # PUT "/users/1" with `fullname=Lindsay+Fünke`
@@ -116,7 +116,7 @@ module Her
116
116
  # @param [Hash] opts Options
117
117
  # @option opts [String] :class_name The name of the class to map objects to
118
118
  # @option opts [Symbol] :data_key The attribute where the data is stored
119
- # @option opts [Path] :path The relative path where to fetch the data (defaults to `/{class_name}.pluralize/{id}`)
119
+ # @option opts [Path] :path The relative path where to fetch the data
120
120
  # @option opts [Symbol] :foreign_key The foreign key used to build the `:id` part of the path (defaults to `{name}_id`)
121
121
  #
122
122
  # @example
@@ -10,8 +10,7 @@ module Her
10
10
  :name => name,
11
11
  :data_key => name,
12
12
  :default => nil,
13
- :foreign_key => "#{name}_id",
14
- :path => "/#{name.to_s.pluralize}/:id"
13
+ :foreign_key => "#{name}_id"
15
14
  }.merge(opts)
16
15
  klass.associations[:belongs_to] << opts
17
16
 
@@ -69,7 +69,15 @@ module Her
69
69
  # user.assign_attributes(name: "Lindsay")
70
70
  # user.changes # => { :name => ["Tobias", "Lindsay"] }
71
71
  def assign_attributes(new_attributes)
72
+ if !new_attributes.respond_to?(:to_hash)
73
+ raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
74
+ end
75
+
76
+ # Coerce new_attributes to hash in case of strong parameters
77
+ new_attributes = new_attributes.to_hash
78
+
72
79
  @_her_attributes ||= attributes
80
+
73
81
  # Use setter methods first
74
82
  unset_attributes = self.class.use_setter_methods(self, new_attributes)
75
83
 
@@ -61,6 +61,16 @@ module Her
61
61
  self
62
62
  end
63
63
 
64
+ # Update a resource and return it
65
+ #
66
+ # @example
67
+ # @user = User.find(1)
68
+ # @user.update_attributes(:name => "Tobias Fünke")
69
+ # # Called via PUT "/users/1"
70
+ def update_attributes(attributes)
71
+ assign_attributes(attributes) && save
72
+ end
73
+
64
74
  # Destroy a resource
65
75
  #
66
76
  # @example
@@ -1,3 +1,3 @@
1
1
  module Her
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -112,8 +112,7 @@ describe Her::Model::Associations do
112
112
  data_key: :organization,
113
113
  default: nil,
114
114
  class_name: "Organization",
115
- foreign_key: "organization_id",
116
- path: "/organizations/:id"
115
+ foreign_key: "organization_id"
117
116
  }
118
117
  end
119
118
  before { Foo::User.belongs_to :organization }
@@ -121,7 +120,8 @@ describe Her::Model::Associations do
121
120
  it { is_expected.to eql [organization_association] }
122
121
  end
123
122
 
124
- context "multiple" do
123
+ context "specifying non-default path" do
124
+ let(:path) { 'my_special_path' }
125
125
  let(:organization_association) do
126
126
  {
127
127
  name: :organization,
@@ -129,7 +129,22 @@ describe Her::Model::Associations do
129
129
  default: nil,
130
130
  class_name: "Organization",
131
131
  foreign_key: "organization_id",
132
- path: "/organizations/:id"
132
+ path: path
133
+ }
134
+ end
135
+ before { Foo::User.belongs_to :organization, path: path }
136
+
137
+ it { is_expected.to eql [organization_association] }
138
+ end
139
+
140
+ context "multiple" do
141
+ let(:organization_association) do
142
+ {
143
+ name: :organization,
144
+ data_key: :organization,
145
+ default: nil,
146
+ class_name: "Organization",
147
+ foreign_key: "organization_id"
133
148
  }
134
149
  end
135
150
  let(:family_association) do
@@ -138,8 +153,7 @@ describe Her::Model::Associations do
138
153
  data_key: :family,
139
154
  default: nil,
140
155
  class_name: "Family",
141
- foreign_key: "family_id",
142
- path: "/families/:id"
156
+ foreign_key: "family_id"
143
157
  }
144
158
  end
145
159
  before do
@@ -216,8 +230,7 @@ describe Her::Model::Associations do
216
230
  data_key: :org,
217
231
  default: true,
218
232
  class_name: "Business",
219
- foreign_key: "org_id",
220
- path: "/organizations/:id"
233
+ foreign_key: "org_id"
221
234
  }
222
235
  end
223
236
  before do
@@ -527,6 +540,74 @@ describe Her::Model::Associations do
527
540
  end
528
541
  end
529
542
 
543
+ context "handling associations with collection_path" do
544
+ before do
545
+ spawn_model "Foo::Organization" do
546
+ has_many :users
547
+ parse_root_in_json true
548
+ collection_path '/special/organizations'
549
+ end
550
+ spawn_model "Foo::User" do
551
+ belongs_to :organization
552
+ end
553
+ end
554
+
555
+ context "without included data" do
556
+ before(:context) do
557
+ Her::API.setup url: "https://api.example.com" do |builder|
558
+ builder.use Her::Middleware::FirstLevelParseJSON
559
+ builder.use Faraday::Request::UrlEncoded
560
+ builder.adapter :test do |stub|
561
+ stub.get("/users/2") { [200, {}, { id: 2, name: "Lindsay Fünke", organization_id: 2 }.to_json] }
562
+ stub.get("/special/organizations/2") { [200, {}, { organization: { id: 2, name: "Bluth Company" } }.to_json] }
563
+ end
564
+ end
565
+ end
566
+
567
+ let(:user) { Foo::User.find(2) }
568
+
569
+ it "fetches data that was not included through belongs_to" do
570
+ expect(user.organization).to be_a(Foo::Organization)
571
+ expect(user.organization.id).to eq(2)
572
+ expect(user.organization.name).to eq("Bluth Company")
573
+ end
574
+ end
575
+ end
576
+
577
+ context "handling associations with path_prefix" do
578
+ before do
579
+ spawn_model "Foo::Organization" do
580
+ has_many :users
581
+ parse_root_in_json true
582
+ end
583
+ spawn_model "Foo::User" do
584
+ belongs_to :organization
585
+ end
586
+ end
587
+
588
+ context "without included data" do
589
+ before(:context) do
590
+ Her::API.setup url: "https://api.example.com" do |builder|
591
+ builder.use Her::Middleware::FirstLevelParseJSON
592
+ builder.use Faraday::Request::UrlEncoded
593
+ builder.path_prefix = 'special'
594
+ builder.adapter :test do |stub|
595
+ stub.get("/special/users/2") { [200, {}, { id: 2, name: "Lindsay Fünke", organization_id: 2 }.to_json] }
596
+ stub.get("/special/organizations/2") { [200, {}, { organization: { id: 2, name: "Bluth Company" } }.to_json] }
597
+ end
598
+ end
599
+ end
600
+
601
+ let(:user) { Foo::User.find(2) }
602
+
603
+ it "fetches data that was not included through belongs_to" do
604
+ expect(user.organization).to be_a(Foo::Organization)
605
+ expect(user.organization.id).to eq(2)
606
+ expect(user.organization.name).to eq("Bluth Company")
607
+ end
608
+ end
609
+ end
610
+
530
611
  context "handling associations with details in active_model_serializers format" do
531
612
  before do
532
613
  spawn_model "Foo::User" do
@@ -86,6 +86,10 @@ describe Her::Model::Attributes do
86
86
  @user.assign_attributes active: true
87
87
  expect(@user).to be_active
88
88
  end
89
+
90
+ it "expects to receive hash" do
91
+ expect { @user.assign_attributes(1) }.to raise_error(ArgumentError, 'When assigning attributes, you must pass a hash as an argument.')
92
+ end
89
93
  end
90
94
 
91
95
  context "checking resource equality" do
@@ -430,6 +430,13 @@ describe Her::Model::ORM do
430
430
  expect(@user.fullname).to eq("Lindsay Fünke")
431
431
  end
432
432
 
433
+ it "handle resource update through #update_attributes" do
434
+ @user = Foo::User.find(1)
435
+ expect(@user).to receive(:save).and_return(true)
436
+ @user.update_attributes(fullname: "Lindsay Fünke")
437
+ expect(@user.fullname).to eq("Lindsay Fünke")
438
+ end
439
+
433
440
  it "handles resource update through #toggle without saving it" do
434
441
  @user = Foo::User.find(1)
435
442
  expect(@user.admin).to be_falsey
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: her
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-18 00:00:00.000000000 Z
11
+ date: 2019-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -111,6 +111,7 @@ files:
111
111
  - ".rspec"
112
112
  - ".rubocop.yml"
113
113
  - ".rubocop_todo.yml"
114
+ - ".ruby-version"
114
115
  - ".travis.yml"
115
116
  - ".yardopts"
116
117
  - CONTRIBUTING.md