ardb 0.23.0 → 0.24.0

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.
@@ -4,11 +4,13 @@ module Ardb
4
4
 
5
5
  attr_reader :applied
6
6
  attr_accessor :limit_value, :offset_value
7
+ attr_accessor :pluck_values
7
8
  attr_accessor :results
8
9
 
9
10
  def initialize
10
11
  @applied, @results = [], []
11
12
  @offset_value, @limit_value = nil, nil
13
+ @pluck_values = {}
12
14
  end
13
15
 
14
16
  def initialize_copy(copied_from)
@@ -128,6 +130,10 @@ module Ardb
128
130
  all.size
129
131
  end
130
132
 
133
+ def pluck(column_name)
134
+ [@pluck_values[column_name]] * @results.size
135
+ end
136
+
131
137
  class AppliedExpression < Struct.new(:type, :args)
132
138
  def to_sql
133
139
  "#{self.type}: #{self.args.inspect}"
@@ -0,0 +1,55 @@
1
+ module Ardb
2
+
3
+ module UseDbDefault
4
+
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ extend ClassMethods
8
+ include InstanceMethods
9
+
10
+ @ardb_use_db_default_attrs = []
11
+
12
+ around_create :ardb_allow_db_to_default_attrs
13
+
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+
19
+ def use_db_default_attrs
20
+ @ardb_use_db_default_attrs
21
+ end
22
+
23
+ def use_db_default(*attrs)
24
+ @ardb_use_db_default_attrs += attrs.map(&:to_s)
25
+ @ardb_use_db_default_attrs.uniq!
26
+ end
27
+
28
+ end
29
+
30
+ module InstanceMethods
31
+
32
+ private
33
+
34
+ def ardb_allow_db_to_default_attrs
35
+ # this allows the attr to be defaulted by the DB, this keeps
36
+ # activerecord from adding the attr into the sql `INSERT`, which will
37
+ # make the DB default its value
38
+ unchanged_names = self.class.use_db_default_attrs.reject do |name|
39
+ self.send("#{name}_changed?")
40
+ end
41
+ unchanged_names.each{ |name| @attributes.delete(name) }
42
+ yield
43
+ # we have to go and fetch the attr value from the DB, otherwise
44
+ # activerecord doesn't know the value that the DB used
45
+ scope = self.class.where(:id => self.id)
46
+ unchanged_names.each do |name|
47
+ @attributes[name] = scope.pluck(name).first
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
data/lib/ardb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ardb
2
- VERSION = "0.23.0"
2
+ VERSION = "0.24.0"
3
3
  end
@@ -13,6 +13,7 @@ class Ardb::RelationSpy
13
13
  should have_readers :applied
14
14
  should have_accessors :results
15
15
  should have_accessors :limit_value, :offset_value
16
+ should have_accessors :pluck_values
16
17
  should have_imeths :to_sql, :reset!
17
18
  should have_imeths :select
18
19
  should have_imeths :from
@@ -24,13 +25,14 @@ class Ardb::RelationSpy
24
25
  should have_imeths :limit, :offset
25
26
  should have_imeths :merge, :only, :except
26
27
  should have_imeths :find, :first, :first!, :last, :last!, :all
27
- should have_imeths :count
28
+ should have_imeths :count, :pluck
28
29
 
29
30
  should "default it's attributes" do
30
31
  assert_equal [], subject.applied
31
32
  assert_equal [], subject.results
32
33
  assert_nil subject.limit_value
33
34
  assert_nil subject.offset_value
35
+ assert_equal({}, subject.pluck_values)
34
36
  end
35
37
 
36
38
  should "dup its applied and results arrays when copied" do
@@ -470,6 +472,21 @@ class Ardb::RelationSpy
470
472
 
471
473
  end
472
474
 
475
+ class PluckTests < WithResultsTests
476
+ desc "pluck"
477
+ setup do
478
+ @column_name = Factory.string
479
+ @column_value = Factory.string
480
+ @relation_spy.pluck_values[@column_name] = @column_value
481
+ end
482
+
483
+ should "return a pluck value for every result" do
484
+ exp = [@column_value] * @results.size
485
+ assert_equal exp, @relation_spy.pluck(@column_name)
486
+ end
487
+
488
+ end
489
+
473
490
  class AppliedExpressionTests < UnitTests
474
491
  desc "AppliedExpression"
475
492
  setup do
@@ -0,0 +1,155 @@
1
+ require 'assert'
2
+ require 'ardb/use_db_default'
3
+
4
+ require 'ardb/record_spy'
5
+
6
+ module Ardb::UseDbDefault
7
+
8
+ class UnitTests < Assert::Context
9
+ desc "Ardb::UseDbDefault"
10
+ setup do
11
+ @record_class = Class.new do
12
+ include UseDbDefaultRecordSpy
13
+ include Ardb::UseDbDefault
14
+ end
15
+ end
16
+ subject{ @record_class }
17
+
18
+ should have_imeths :use_db_default, :use_db_default_attrs
19
+
20
+ should "know its use db default attrs" do
21
+ assert_equal [], subject.use_db_default_attrs
22
+ end
23
+
24
+ should "add use db default attributes using `use_db_default`" do
25
+ attr_name = Factory.string
26
+ subject.use_db_default(attr_name)
27
+ assert_includes attr_name, subject.use_db_default_attrs
28
+
29
+ attr_names = [Factory.string, Factory.string.to_sym]
30
+ subject.use_db_default(*attr_names)
31
+ attr_names.each do |attr_name|
32
+ assert_includes attr_name.to_s, subject.use_db_default_attrs
33
+ end
34
+ end
35
+
36
+ should "not add duplicate attributes using `use_db_default`" do
37
+ attr_name = Factory.string
38
+ subject.use_db_default(attr_name)
39
+ assert_equal [attr_name], subject.use_db_default_attrs
40
+
41
+ subject.use_db_default(attr_name)
42
+ assert_equal [attr_name], subject.use_db_default_attrs
43
+
44
+ more_attr_names = [attr_name, Factory.string]
45
+ subject.use_db_default(*more_attr_names)
46
+ exp = ([attr_name] + more_attr_names).uniq
47
+ assert_equal exp, subject.use_db_default_attrs
48
+ end
49
+
50
+ should "add an around create callback" do
51
+ callback = subject.callbacks.first
52
+ assert_equal :around_create, callback.type
53
+ exp = [:ardb_allow_db_to_default_attrs]
54
+ assert_equal exp, callback.args
55
+ end
56
+
57
+ end
58
+
59
+ class InitTests < UnitTests
60
+ desc "when init"
61
+ setup do
62
+ @attr_names = Factory.integer(3).times.map{ Factory.string }
63
+ @record_class.use_db_default(*@attr_names)
64
+
65
+ @record = @record_class.new
66
+
67
+ # simulate activerecords `@attributes` hash
68
+ @original_attrs = @attr_names.inject({}) do |h, n|
69
+ h.merge!(n => [nil, Factory.string, Factory.integer].choice)
70
+ end
71
+ @original_attrs.merge!(Factory.string => Factory.string)
72
+ @record.attributes = @original_attrs.dup
73
+
74
+ # randomly pick a use-db-default attribute to be changed
75
+ @record.changed_use_db_default_attrs = [@attr_names.choice]
76
+ @unchanged_attr_names = @attr_names - @record.changed_use_db_default_attrs
77
+
78
+ # we should always get the record we just inserted back
79
+ @record_class.relation_spy.results = [@record]
80
+ # add pluck values into the relation spy
81
+ @record_class.relation_spy.pluck_values = @attr_names.inject({}) do |h, n|
82
+ h.merge!(n => Factory.string)
83
+ end
84
+ end
85
+ subject{ @record }
86
+
87
+ should "remove use-db-default attributes before being created" do
88
+ # around create callbacks yield to create the record so we have to pass a
89
+ # block, this will allow us to see what was done to the attributes hash
90
+ # when it was created (yielded)
91
+ attrs_before_yield = nil
92
+ subject.instance_eval do
93
+ ardb_allow_db_to_default_attrs{ attrs_before_yield = self.attributes.dup }
94
+ end
95
+
96
+ assert_instance_of Hash, attrs_before_yield
97
+ @unchanged_attr_names.each do |name|
98
+ assert_false attrs_before_yield.key?(name)
99
+ end
100
+
101
+ # the non use-db-default attrs and changed use-db-default attrs hash
102
+ # values should have their original values when yielded
103
+ (@original_attrs.keys - @unchanged_attr_names).each do |name|
104
+ assert_equal @original_attrs[name], attrs_before_yield[name]
105
+ end
106
+ end
107
+
108
+ should "set use-db-default values after its created" do
109
+ # around create callbacks yield to create the record so we have to pass a
110
+ # block, this simulates creating the record by setting the id
111
+ subject.instance_eval do
112
+ ardb_allow_db_to_default_attrs{ self.id = Factory.integer }
113
+ end
114
+
115
+ applied_expr = @record_class.relation_spy.applied.first
116
+ assert_equal :where, applied_expr.type
117
+ assert_equal [{ :id => subject.id }], applied_expr.args
118
+
119
+ @unchanged_attr_names.each do |name|
120
+ exp = @record_class.relation_spy.pluck_values[name]
121
+ assert_equal exp, subject.attributes[name]
122
+ end
123
+
124
+ # the non use-db-default attrs and changed use-db-default attrs hash
125
+ # values should still have their original values
126
+ (@original_attrs.keys - @unchanged_attr_names).each do |name|
127
+ assert_equal @original_attrs[name], subject.attributes[name]
128
+ end
129
+ end
130
+
131
+ end
132
+
133
+ module UseDbDefaultRecordSpy
134
+ def self.included(klass)
135
+ klass.class_eval{ include Ardb::RecordSpy }
136
+ end
137
+
138
+ attr_accessor :id
139
+ attr_accessor :attributes, :changed_use_db_default_attrs
140
+
141
+ def use_db_default_attr_changed?(attr_name)
142
+ self.changed_use_db_default_attrs.include?(attr_name)
143
+ end
144
+
145
+ def method_missing(method, *args, &block)
146
+ match_data = method.to_s.match(/(\w+)_changed\?/)
147
+ if match_data && match_data[1]
148
+ self.use_db_default_attr_changed?(match_data[1])
149
+ else
150
+ super
151
+ end
152
+ end
153
+ end
154
+
155
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ardb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 67
4
+ hash: 127
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 23
8
+ - 24
9
9
  - 0
10
- version: 0.23.0
10
+ version: 0.24.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2015-10-13 00:00:00 Z
19
+ date: 2015-10-22 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -129,6 +129,7 @@ files:
129
129
  - lib/ardb/runner/generate_command.rb
130
130
  - lib/ardb/runner/migrate_command.rb
131
131
  - lib/ardb/test_helpers.rb
132
+ - lib/ardb/use_db_default.rb
132
133
  - lib/ardb/version.rb
133
134
  - log/.gitkeep
134
135
  - test/helper.rb
@@ -150,6 +151,7 @@ files:
150
151
  - test/unit/runner/migrate_command_tests.rb
151
152
  - test/unit/runner_tests.rb
152
153
  - test/unit/test_helpers_tests.rb
154
+ - test/unit/use_db_default_tests.rb
153
155
  - tmp/.gitkeep
154
156
  - tmp/mysqltest/.gitkeep
155
157
  - tmp/pgtest/.gitkeep
@@ -216,3 +218,4 @@ test_files:
216
218
  - test/unit/runner/migrate_command_tests.rb
217
219
  - test/unit/runner_tests.rb
218
220
  - test/unit/test_helpers_tests.rb
221
+ - test/unit/use_db_default_tests.rb