protobuf-activerecord 3.4.3 → 3.4.4.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40a8056f1bb9bf2ac10f53ea39f1447e909a3227
4
- data.tar.gz: 11b9ffcbe3645dbe06bc677730fe360cb66d0f5b
3
+ metadata.gz: da307ffdb292153c946b5e4d6fb51c601d19f887
4
+ data.tar.gz: '0548445446f601abd66b34b34cab4bbe8f4237b3'
5
5
  SHA512:
6
- metadata.gz: cc1d009811935c2ad4026862e8f20f00ddc51e8f1220a5a309a0e3c5d25654e817e1363220b49a8c49bfd60ad931fcb884fa3ecaa2d73c7ad713edda0b0d5e0d
7
- data.tar.gz: 824c943dadf8071ce786129a52e051a61c345a8c6b1cac6e420cdabac0e21360b23d79a273abe6e59ab8403671725dcccd1b34fca979c7c87527643c402946f7
6
+ metadata.gz: 7779dd21a08bde0e8dc52ee6cc84170811dc59ad13dbc4ee43a46c04ffc41912620c2599964ecc20452432f6159c2367eac0203a7c41bc6fe958dae65da278d1
7
+ data.tar.gz: 972422ff88ff326053604bf64b15cd0c63360146960ea815c81beb7684dbf535282aa27b8e4b640deec9890d6bb7d27cf6846005b4ac1aabe282d89c5cc5e33c
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
3
 
4
4
  desc "Run specs"
5
5
  RSpec::Core::RakeTask.new(:spec)
@@ -16,16 +16,39 @@ module Protobuf
16
16
  end
17
17
 
18
18
  module ClassMethods
19
- # :nodoc
19
+ # :nodoc:
20
20
  def accepts_nested_attributes_for(*attr_names)
21
21
  attribute_names = attr_names.dup
22
22
  attribute_names.extract_options!
23
+ attribute_names.map!(&:to_s)
23
24
 
24
25
  super
25
26
 
26
- self._protobuf_nested_attributes += attribute_names.map { |name| "#{name}_attributes" }
27
+ self._protobuf_nested_attributes += attribute_names
27
28
  end
28
29
  end
30
+
31
+ # :nodoc:
32
+ def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
33
+ if attributes_collection.first.is_a?(::Protobuf::Message)
34
+ reflection = self.class._reflect_on_association(association_name)
35
+ attributes_collection = attributes_collection.map do |attributes|
36
+ reflection.klass.attributes_from_proto(attributes)
37
+ end
38
+ end
39
+
40
+ super(association_name, attributes_collection)
41
+ end
42
+
43
+ # :nodoc:
44
+ def assign_nested_attributes_for_one_to_one_association(association_name, attributes)
45
+ if attributes.is_a?(::Protobuf::Message)
46
+ reflection = self.class._reflect_on_association(association_name)
47
+ attributes = reflection.klass.attributes_from_proto(attributes)
48
+ end
49
+
50
+ super(association_name, attributes)
51
+ end
29
52
  end
30
53
  end
31
54
  end
@@ -33,20 +33,30 @@ module Protobuf
33
33
  proto.field?(key) && !field.repeated?
34
34
  end
35
35
 
36
- filtered_attributes = _filtered_attributes + _protobuf_nested_attributes
37
- filtered_attributes += _protobuf_attribute_transformers.keys
36
+ filtered_attributes = _filtered_attributes + _protobuf_attribute_transformers.keys
38
37
 
39
38
  attribute_fields = filtered_attributes.inject({}) do |hash, column_name|
40
39
  symbolized_column = column_name.to_sym
41
40
 
42
- if fields.has_key?(symbolized_column) ||
43
- _protobuf_attribute_transformers.has_key?(symbolized_column)
41
+ if fields.has_key?(symbolized_column) || _protobuf_attribute_transformers.has_key?(symbolized_column)
44
42
  hash[symbolized_column] = fields[symbolized_column]
45
43
  end
46
44
 
47
45
  hash
48
46
  end
49
47
 
48
+ _protobuf_nested_attributes.each do |attribute_name|
49
+ nested_attribute_name = "#{attribute_name}_attributes".to_sym
50
+ value = if proto.has_field?(nested_attribute_name)
51
+ proto.__send__(nested_attribute_name)
52
+ elsif proto.has_field?(attribute_name)
53
+ proto.__send__(attribute_name)
54
+ end
55
+
56
+ next unless value
57
+ attribute_fields[nested_attribute_name] = value
58
+ end
59
+
50
60
  attribute_fields
51
61
  end
52
62
 
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "3.4.3"
3
+ VERSION = "3.4.4.pre"
4
4
  end
5
5
  end
@@ -0,0 +1,29 @@
1
+ require "spec_helper"
2
+
3
+ describe Protobuf::ActiveRecord::NestedAttributes do
4
+ let(:user_message) {
5
+ UserMessage.new(:name => "foo bar", :email => "foo@test.co", :photos => [{ url: "https://test.co/test.png" }])
6
+ }
7
+
8
+ describe "._filter_attribute_fields", :aggregate_failures => true do
9
+ it "includes nested attributes" do
10
+ attribute_fields = User._filter_attribute_fields(user_message)
11
+ expect(attribute_fields[:photos_attributes]).to eq(user_message.photos)
12
+ end
13
+
14
+ context "when" do
15
+
16
+ end
17
+ end
18
+
19
+ describe ".new" do
20
+ context "when a model accepts nested attributes" do
21
+ it "transforms nested attributes", :aggregate_failures => true do
22
+ user_message.photos.each do |photo_message|
23
+ expect(Photo).to receive(:attributes_from_proto).with(photo_message).and_call_original
24
+ end
25
+ User.new(user_message)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -9,7 +9,7 @@ describe Protobuf::ActiveRecord::Serialization do
9
9
  let(:protobuf_message) { UserMessage }
10
10
 
11
11
  describe ".field_from_record" do
12
- context "when the given converter is a symbol" do
12
+ context "when the given transformer is a symbol" do
13
13
  before { User.field_from_record :first_name, :extract_first_name }
14
14
 
15
15
  it "creates a symbol transformer from the converter" do
@@ -17,7 +17,7 @@ describe Protobuf::ActiveRecord::Serialization do
17
17
  end
18
18
  end
19
19
 
20
- context "when the given converter is not callable" do
20
+ context "when the given transformer is not callable" do
21
21
  it "raises an exception" do
22
22
  expect { User.field_from_record :name, nil }.to raise_exception(Protobuf::ActiveRecord::FieldTransformerError)
23
23
  end
@@ -31,9 +31,8 @@ describe Protobuf::ActiveRecord::Serialization do
31
31
  User.field_from_record :account_id, callable
32
32
  }
33
33
 
34
- it "adds the given converter to the list of protobuf field transformers", :pending => 'missing expectation?' do
35
- User._protobuf_field_transformers[:account_id] = callable
36
- fail
34
+ it "adds the given converter to the list of protobuf field transformers" do
35
+ expect(User._protobuf_field_transformers[:account_id]).to eq(callable)
37
36
  end
38
37
  end
39
38
  end
@@ -78,26 +77,26 @@ describe Protobuf::ActiveRecord::Serialization do
78
77
  context "when options has :except" do
79
78
  it "returns all except the given field(s)" do
80
79
  fields = user._filter_field_attributes(:except => :name)
81
- expect(fields).to match_array([ :guid, :email, :email_domain, :password, :nullify ])
80
+ expect(fields).to match_array([ :guid, :email, :email_domain, :password, :nullify, :photos ])
82
81
  end
83
82
  end
84
83
  end
85
84
 
86
85
  describe "#_filtered_fields" do
87
86
  it "returns protobuf fields" do
88
- expect(user._filtered_fields).to match_array([ :guid, :name, :email, :email_domain, :password, :nullify ])
87
+ expect(user._filtered_fields).to match_array([ :guid, :name, :email, :email_domain, :password, :nullify, :photos ])
89
88
  end
90
89
 
91
90
  context "given :deprecated => false" do
92
91
  it "filters all deprecated fields" do
93
92
  fields = user._filtered_fields(:deprecated => false)
94
- expect(fields).to match_array([ :guid, :name, :email, :password, :nullify ])
93
+ expect(fields).to match_array([ :guid, :name, :email, :password, :nullify, :photos ])
95
94
  end
96
95
 
97
96
  context 'and :include => :email_domain' do
98
97
  it 'includes deprecated fields that have been explicitly specified' do
99
98
  fields = user._filtered_fields(:deprecated => false, :include => :email_domain)
100
- expect(fields).to match_array([ :guid, :name, :email, :email_domain, :password, :nullify ])
99
+ expect(fields).to match_array([ :guid, :name, :email, :email_domain, :password, :nullify, :photos ])
101
100
  end
102
101
  end
103
102
  end
@@ -155,23 +154,8 @@ describe Protobuf::ActiveRecord::Serialization do
155
154
  }
156
155
 
157
156
  context "when a transformer is defined for the field" do
158
- let(:fields_from_record) { { :guid => user.guid, :name => user.name, :email => user.email, :email_domain => 'test.co', :password => nil, :nullify => nil } }
159
-
160
157
  it "gets the field from the transformer" do
161
- expect(user.fields_from_record).to eq fields_from_record
162
- end
163
- end
164
-
165
- context "when a transformer is not defined for the field" do
166
- let(:fields_from_record) { { :guid => user.guid, :name => user.name, :email => user.email, :email_domain => nil, :password => nil, :nullify => nil } }
167
-
168
- before {
169
- user.to_proto
170
- allow(user).to receive(:_protobuf_active_record_serialize_email_domain).and_return(nil)
171
- }
172
-
173
- it "returns a hash of protobuf fields that this object has getters for" do
174
- expect(user.fields_from_record).to eq fields_from_record
158
+ expect(user.fields_from_record[:email_domain]).to eq('test.co')
175
159
  end
176
160
  end
177
161
 
@@ -201,14 +201,11 @@ describe Protobuf::ActiveRecord::Transformation do
201
201
  context "when the given transformer is callable" do
202
202
  let(:callable) { lambda { |proto| nil } }
203
203
 
204
- before {
205
- allow(User).to receive(:_protobuf_attribute_transformers).and_return(Hash.new)
206
- User.attribute_from_proto :account_id, callable
207
- }
204
+ before { allow(User).to receive(:_protobuf_attribute_transformers).and_return(Hash.new) }
208
205
 
209
- it "adds the given converter to the list of protobuf field transformers", :pending => 'missing expectation?' do
210
- User._protobuf_attribute_transformers[:account_id] = callable
211
- fail
206
+ it "adds the given converter to the list of protobuf field transformers" do
207
+ User.attribute_from_proto :account_id, callable
208
+ expect(User._protobuf_attribute_transformers[:account_id].callable).to eq callable
212
209
  end
213
210
  end
214
211
  end
data/spec/spec_helper.rb CHANGED
@@ -1,19 +1,19 @@
1
- require 'rubygems'
2
- require 'bundler'
1
+ require "rubygems"
2
+ require "bundler"
3
3
 
4
- require 'simplecov'
4
+ require "simplecov"
5
5
  SimpleCov.start do
6
- add_filter '/spec/'
6
+ add_filter "/spec/"
7
7
  end
8
8
 
9
9
  Bundler.require(:default, :development, :test)
10
10
 
11
- require 'support/db'
12
- require 'support/models'
13
- require 'support/protobuf'
11
+ require "support/db"
12
+ require "support/models"
13
+ require "support/protobuf/messages.pb"
14
14
 
15
- # Silence protobuf's logger
16
- ::Protobuf::Logging.logger.level = ::Logger::FATAL
15
+ # Silence protobuf"s logger
16
+ Protobuf::Logging.logger.level = ::Logger::FATAL
17
17
 
18
18
  RSpec.configure do |config|
19
19
  # Turn deprecation warnings into errors with full backtrace.
@@ -10,6 +10,13 @@ ActiveRecord::Base.connection.tables.each do |table|
10
10
  end
11
11
 
12
12
  ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :photos do |t|
14
+ t.string :url
15
+ t.integer :user_id
16
+
17
+ t.timestamps null: false
18
+ end
19
+
13
20
  create_table :users do |t|
14
21
  t.string :guid
15
22
  t.string :first_name
@@ -17,6 +24,6 @@ ActiveRecord::Schema.define(:version => 1) do
17
24
  t.string :email
18
25
  t.integer :account_id
19
26
 
20
- t.timestamps
27
+ t.timestamps null: false
21
28
  end
22
29
  end
@@ -1,3 +1,8 @@
1
+ message PhotoMessage {
2
+ optional string url = 1;
3
+ optional int64 user_guid = 2;
4
+ }
5
+
1
6
  message UserMessage {
2
7
  optional string guid = 1;
3
8
  optional string name = 2;
@@ -5,6 +10,7 @@ message UserMessage {
5
10
  optional string email_domain = 4 [deprecated = true];
6
11
  optional string password = 5;
7
12
  repeated string nullify = 6;
13
+ repeated PhotoMessage photos = 7;
8
14
  }
9
15
 
10
16
  message UserSearchMessage {
@@ -1 +1,2 @@
1
- require 'support/models/user'
1
+ require 'support/models/photo'
2
+ require 'support/models/user'
@@ -0,0 +1,3 @@
1
+ class Photo < ActiveRecord::Base
2
+ include Protobuf::ActiveRecord::Model
3
+ end
@@ -3,9 +3,15 @@ class User < ActiveRecord::Base
3
3
 
4
4
  attr_accessor :password
5
5
 
6
+ has_many :photos
7
+
8
+ accepts_nested_attributes_for :photos
9
+
6
10
  scope :by_guid, lambda { |*guids| where(:guid => guids) }
7
11
  scope :by_email, lambda { |*emails| where(:email => emails) }
8
12
 
13
+ protobuf_fields :except => :photos
14
+
9
15
  attribute_from_proto :first_name, :extract_first_name
10
16
  attribute_from_proto :last_name, :extract_last_name
11
17
  attribute_from_proto :password, lambda { |proto| proto.password! }
@@ -1,12 +1,15 @@
1
+ # encoding: utf-8
2
+
1
3
  ##
2
4
  # This file is auto-generated. DO NOT EDIT!
3
5
  #
4
- require 'protobuf/message'
6
+ require 'protobuf'
5
7
 
6
8
 
7
9
  ##
8
10
  # Message Classes
9
11
  #
12
+ class PhotoMessage < ::Protobuf::Message; end
10
13
  class UserMessage < ::Protobuf::Message; end
11
14
  class UserSearchMessage < ::Protobuf::Message; end
12
15
 
@@ -14,6 +17,11 @@ class UserSearchMessage < ::Protobuf::Message; end
14
17
  ##
15
18
  # Message Fields
16
19
  #
20
+ class PhotoMessage
21
+ optional :string, :url, 1
22
+ optional :int64, :user_guid, 2
23
+ end
24
+
17
25
  class UserMessage
18
26
  optional :string, :guid, 1
19
27
  optional :string, :name, 2
@@ -21,6 +29,7 @@ class UserMessage
21
29
  optional :string, :email_domain, 4, :deprecated => true
22
30
  optional :string, :password, 5
23
31
  repeated :string, :nullify, 6
32
+ repeated ::PhotoMessage, :photos, 7
24
33
  end
25
34
 
26
35
  class UserSearchMessage
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.3
4
+ version: 3.4.4.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hutchison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-22 00:00:00.000000000 Z
11
+ date: 2017-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -216,6 +216,7 @@ files:
216
216
  - lib/protobuf/active_record/version.rb
217
217
  - protobuf-activerecord.gemspec
218
218
  - spec/protobuf/active_record/columns_spec.rb
219
+ - spec/protobuf/active_record/nested_attributes_spec.rb
219
220
  - spec/protobuf/active_record/persistence_spec.rb
220
221
  - spec/protobuf/active_record/scope_spec.rb
221
222
  - spec/protobuf/active_record/serialization_spec.rb
@@ -224,11 +225,11 @@ files:
224
225
  - spec/spec_helper.rb
225
226
  - spec/support/db.rb
226
227
  - spec/support/db/setup.rb
227
- - spec/support/definitions/user.proto
228
+ - spec/support/definitions/messages.proto
228
229
  - spec/support/models.rb
230
+ - spec/support/models/photo.rb
229
231
  - spec/support/models/user.rb
230
- - spec/support/protobuf.rb
231
- - spec/support/protobuf/user.pb.rb
232
+ - spec/support/protobuf/messages.pb.rb
232
233
  homepage: http://github.com/liveh2o/protobuf-activerecord
233
234
  licenses:
234
235
  - MIT
@@ -244,17 +245,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
244
245
  version: '0'
245
246
  required_rubygems_version: !ruby/object:Gem::Requirement
246
247
  requirements:
247
- - - ">="
248
+ - - ">"
248
249
  - !ruby/object:Gem::Version
249
- version: '0'
250
+ version: 1.3.1
250
251
  requirements: []
251
252
  rubyforge_project:
252
- rubygems_version: 2.6.12
253
+ rubygems_version: 2.6.13
253
254
  signing_key:
254
255
  specification_version: 4
255
256
  summary: Google Protocol Buffers integration for Active Record
256
257
  test_files:
257
258
  - spec/protobuf/active_record/columns_spec.rb
259
+ - spec/protobuf/active_record/nested_attributes_spec.rb
258
260
  - spec/protobuf/active_record/persistence_spec.rb
259
261
  - spec/protobuf/active_record/scope_spec.rb
260
262
  - spec/protobuf/active_record/serialization_spec.rb
@@ -263,8 +265,8 @@ test_files:
263
265
  - spec/spec_helper.rb
264
266
  - spec/support/db.rb
265
267
  - spec/support/db/setup.rb
266
- - spec/support/definitions/user.proto
268
+ - spec/support/definitions/messages.proto
267
269
  - spec/support/models.rb
270
+ - spec/support/models/photo.rb
268
271
  - spec/support/models/user.rb
269
- - spec/support/protobuf.rb
270
- - spec/support/protobuf/user.pb.rb
272
+ - spec/support/protobuf/messages.pb.rb
@@ -1 +0,0 @@
1
- require 'support/protobuf/user.pb'