acts_as_nosql 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: 6557e20a7d3813b7250a1a6b4e346d07e3b8f89b9ad984d66c6632e8e172bf9e
4
- data.tar.gz: 8ddacccc9e46e5102dc4056fd4a139e6dde8302e15bd108f66ab7b15214880e7
3
+ metadata.gz: 932b7a11efc91e47391a1aefa78baebe7fa8cb32d540db3af678e857a46dceaf
4
+ data.tar.gz: 5d4333d029ff28bee728b3e7b1fd565ac18e44413be9d4ebd355234505501bcc
5
5
  SHA512:
6
- metadata.gz: cf9a0fdfba234f852ffd8fc97fe18b709eaff996037da995bccb672696f03d749ac52e4dc2a82306c87f8bb12efd4f7d9f942a43718bb86d309d6b169fd868dd
7
- data.tar.gz: 39662c4f73d0d4f1468a1e02d95ddb6624a907079a9ae4984b449356e51bc5ebf631c162508946f330035747591131f469249e8c72b5484ef0fc6c569aca3e5f
6
+ metadata.gz: 48dce7dc8ee54cb447f7cd1172d563d97acb6faadc931aac0e3cc1780e927e0315b5d5e448252f766231010838ec98542e9996549b88cc898e5a1d92720c1814
7
+ data.tar.gz: '090a9ac2ae392d8f8750d0b2162c2969e3cebd401e7a5b5ad1a6b76b56c4ed1335c28dc9c72542d390a64460882499d83ab0596e9b46df285161703052215378'
@@ -4,10 +4,10 @@ module ActsAsNosql
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- after_initialize :_acts_as_nosql_init
7
+ after_initialize :_acts_as_nosql_init_defaults
8
8
  end
9
9
 
10
- def _acts_as_nosql_init
10
+ def _acts_as_nosql_init_defaults
11
11
  self.class.nosql_attributes.each do |name, attribute|
12
12
  public_send("#{name}=", attribute.default.dup) if public_send(name).nil? && !attribute.default.nil?
13
13
  end
@@ -22,7 +22,7 @@ module ActsAsNosql
22
22
  attribute = self._acts_as_nosql_options[:field_name]
23
23
 
24
24
  names.each do |name|
25
- raise "Attribute #{name} already defined" if instance_methods.include?(name.to_sym) || name.to_sym == attribute.to_sym
25
+ raise "Attribute #{name} already defined" if _acts_as_nosql_attribute_defined?(name)
26
26
 
27
27
  nosql_attributes[name] = ActsAsNosql::Attribute.new(name, type: type, default: default, path: path)
28
28
  _acts_as_nosql_define_attribute(nosql_attributes[name])
@@ -33,12 +33,36 @@ module ActsAsNosql
33
33
  self._acts_as_nosql_options[:attributes] ||= {}
34
34
  end
35
35
 
36
+ def connection
37
+ unless acts_as_nosql_conflicts_checked?
38
+ @acts_as_nosql_conflicts_checked = true
39
+ acts_as_nosql_check_conflicts!
40
+ end
41
+ super
42
+ end
43
+
44
+ def acts_as_nosql_conflicts_checked?
45
+ @acts_as_nosql_conflicts_checked ||= false
46
+ end
47
+
48
+ def acts_as_nosql_check_conflicts!
49
+ columns_map = columns.index_by(&:name)
50
+ nosql_attributes.each do |name, attribute|
51
+ raise "Attribute #{name} already defined" if columns_map[name.to_s]
52
+ end
53
+ end
54
+
36
55
  private
37
56
 
38
57
  def nosql_data_attribute
39
58
  @nosql_data_attribute ||= self._acts_as_nosql_options[:field_name]
40
59
  end
41
60
 
61
+ def _acts_as_nosql_attribute_defined?(name)
62
+ instance_methods.include?(name.to_sym) ||
63
+ name.to_sym == nosql_data_attribute.to_sym
64
+ end
65
+
42
66
  def _acts_as_nosql_define_attribute(nosql_attribute)
43
67
  attribute = nosql_data_attribute
44
68
 
@@ -1,3 +1,3 @@
1
1
  module ActsAsNosql
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
@@ -34,11 +34,45 @@ describe 'Fields declaration' do
34
34
  expect { Article.nosql_attr :data }.to raise_error("Attribute data already defined")
35
35
  end
36
36
 
37
+ it 'raises error if there\'s a name conflict an actual column' do
38
+ expect { Article.nosql_attr :title }.to raise_error("Attribute title already defined")
39
+ end
40
+
41
+ it 'raises error if there\'s a name conflict an existing nosql attribute' do
42
+ expect { Article.nosql_attr :body }.to raise_error("Attribute body already defined")
43
+ end
44
+
37
45
  it 'raises error if there\'s a name conflict' do
38
46
  expect { Article.nosql_attr :some_column }.to raise_error("Attribute some_column already defined")
39
47
  end
40
48
 
41
- it 'fileds are saved as string' do
49
+ it 'raises error if there\'s a name conflict when calling #acts_as_nosql_check_conflicts!' do
50
+ expect do
51
+ class ConflictingArticle < ActiveRecord::Base
52
+ self.table_name = 'articles'
53
+ acts_as_nosql field_name: :data
54
+
55
+ nosql_attrs :title, :editor, type: String
56
+ end
57
+ end.not_to raise_error("Attribute title already defined")
58
+
59
+ expect { ConflictingArticle.acts_as_nosql_check_conflicts! }.to raise_error("Attribute title already defined")
60
+ end
61
+
62
+ it 'raises error if there\'s a name conflict when the model columns are loaded' do
63
+ expect do
64
+ class ConflictingArticle2 < ActiveRecord::Base
65
+ self.table_name = 'articles'
66
+ acts_as_nosql field_name: :data
67
+
68
+ nosql_attrs :title, :editor, type: String
69
+ end
70
+ end.not_to raise_error("Attribute title already defined")
71
+
72
+ expect { ConflictingArticle2.new }.to raise_error("Attribute title already defined")
73
+ end
74
+
75
+ it 'fields are saved as string' do
42
76
  subject.editor = 'John Doe'
43
77
  subject.save!
44
78
  subject.reload
@@ -16,9 +16,16 @@ describe 'Querying' do
16
16
 
17
17
  it 'can be queried' do
18
18
  query = Article.where(body: 'body')
19
- # expect(query.to_sql).to eq(
20
- # "SELECT \"articles\".* FROM \"articles\" WHERE (\"articles\".\"data\"->>\"body\" = 'body')"
21
- # )
19
+ expect(query.to_sql).to include(
20
+ case ENV['ACTIVE_RECORD_ADAPTER']
21
+ when 'postgresql'
22
+ "SELECT \"articles\".* FROM \"articles\" WHERE (\"articles\".\"data\"->>'body' = 'body')"
23
+ when 'mysql'
24
+ "SELECT `articles`.* FROM `articles` WHERE (`articles`.`data`->>'$.body' = 'body')"
25
+ else
26
+ "SELECT \"articles\".* FROM \"articles\" WHERE (\"articles\".\"data\"->>'$.body' = 'body')"
27
+ end
28
+ )
22
29
  expect(query.to_a).to contain_exactly(article)
23
30
  end
24
31
  end
@@ -36,9 +43,16 @@ describe 'Querying' do
36
43
 
37
44
  it 'can query nested attributes' do
38
45
  query = Setting.where(user_auth_token: '123123')
39
- # expect(query.to_sql).to eq(
40
- # "SELECT \"settings\".* FROM \"settings\" WHERE (\"settings\".\"config\"->>\"user\"->>\"auth\"->>\"token\" = '123123')"
41
- # )
46
+ expect(query.to_sql).to eq(
47
+ case ENV['ACTIVE_RECORD_ADAPTER']
48
+ when 'postgresql'
49
+ "SELECT \"settings\".* FROM \"settings\" WHERE (\"settings\".\"config\"->'user'->'auth'->>'token' = '123123')"
50
+ when 'mysql'
51
+ "SELECT `settings`.* FROM `settings` WHERE (`settings`.`config`->>'$.user.auth.token' = '123123')"
52
+ else
53
+ "SELECT \"settings\".* FROM \"settings\" WHERE (\"settings\".\"config\"->>'$.user.auth.token' = '123123')"
54
+ end
55
+ )
42
56
  expect(query.to_a).to contain_exactly(setting)
43
57
  end
44
58
  end
@@ -4,6 +4,7 @@ if ENV['ACTIVE_RECORD_ADAPTER'] == 'mysql'
4
4
  puts 'Running on MySQL...'
5
5
  ActiveRecord::Base.establish_connection(
6
6
  adapter: 'mysql2',
7
+ host: ENV['DB_HOST'] || '127.0.0.1',
7
8
  username: ENV['DB_USERNAME'] || 'root',
8
9
  password: ENV['DB_PASSWORD'],
9
10
  database: 'acts_as_nosql'
@@ -14,8 +15,8 @@ elsif ENV['ACTIVE_RECORD_ADAPTER'] == 'postgresql'
14
15
  adapter: 'postgresql',
15
16
  database: 'acts_as_nosql',
16
17
  host: ENV['DB_HOST'] || '127.0.0.1',
17
- username: ENV['DB_USERNAME'] || 'postgres',
18
- password: ENV['DB_PASSWORD']
18
+ username: ENV['DB_USERNAME'] || ENV['POSTGRES_USER'] || 'postgres',
19
+ password: ENV['DB_PASSWORD'] || ENV['POSTGRES_PASSWORD']
19
20
  )
20
21
  else
21
22
  puts 'Running on SQLite...'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_nosql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mònade