ar-ondemand 1.0.5 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile.lock +1 -1
- data/lib/ar-ondemand/for_reading.rb +8 -1
- data/lib/ar-ondemand/on_demand.rb +23 -9
- data/lib/ar-ondemand/record.rb +17 -7
- data/lib/ar-ondemand/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Yzk3YzYxMjU4NmM2MzQ2MDczNWRmM2JjMDkwYzQ1ZTQ3M2E4OWMzMg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OGQyZDMxNDY3Zjg1YWVmMDcwNDgwNzYwOWQ0NTk0MjkyOTM0MjQ0Ng==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NmY1YzAzZjI4MDY5MThhMjc5NjVjZmFiOTE0YjIyMTVlNjE0ZTRhZmM5ZDFi
|
10
|
+
MDVjMGIzNDRkZDJhMjkxMjcwNzY2NjI0MTg1MjBkZDIxZTcxYjJhOWE5ZDcx
|
11
|
+
Y2IxZmFmZDc3NDQ0YWVjYWZkM2E1ZjNmMjU4NThlYjdhNTA2MjM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZGM5NzJhYTlhZDg0NGJhNWNjNGRmYzQ2YTc5NTc3Y2ZkYmE4NWI5MTQ0OGE4
|
14
|
+
MThhZTk1NzgyYjc1Njc5OTI5YTA0MjhkODQ0YTAyYzJjOTRlNTA4YWFjYTU0
|
15
|
+
N2E1OWYwM2IyY2FmMTZiZTdhNzkyNWQ5OGQyN2M0OTM0ZTE3ZjQ=
|
data/Gemfile.lock
CHANGED
@@ -9,7 +9,14 @@ module ActiveRecord
|
|
9
9
|
|
10
10
|
# Ripped from the find_in_batches function, but customized to return an ::ActiveRecord::OnDemand::ResultSet
|
11
11
|
def for_reading(options = {})
|
12
|
-
|
12
|
+
if options.empty?
|
13
|
+
res = query_for_reading(self)
|
14
|
+
if block_given?
|
15
|
+
yield res
|
16
|
+
return
|
17
|
+
end
|
18
|
+
return res
|
19
|
+
end
|
13
20
|
|
14
21
|
relation = self
|
15
22
|
|
@@ -10,8 +10,8 @@ module ActiveRecord
|
|
10
10
|
def initialize(model, results, key_column, defaults)
|
11
11
|
super(model, results)
|
12
12
|
|
13
|
-
raise
|
14
|
-
raise
|
13
|
+
raise 'Key column cannot be blank.' if key_column.blank?
|
14
|
+
raise 'Defaults cannot be empty.' if defaults.empty?
|
15
15
|
|
16
16
|
@key_column = key_column.to_s
|
17
17
|
@defaults = defaults
|
@@ -26,16 +26,30 @@ module ActiveRecord
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def [](key)
|
29
|
-
raise
|
29
|
+
raise 'Search key cannot be blank.' if key.blank?
|
30
30
|
rec = @results.rows.find { |x| x[@key_index] == key }
|
31
31
|
if rec.nil?
|
32
|
-
if @has_any_assets
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
rec = if @has_any_assets
|
33
|
+
args = @defaults.values + [key]
|
34
|
+
@model.unscoped.send(@find_by_method, *args)
|
35
|
+
else
|
36
|
+
@new_params[@key_column.to_sym] = key
|
37
|
+
@model.new @new_params
|
38
|
+
end
|
39
|
+
|
40
|
+
unless rec.persisted?
|
41
|
+
# These are not getting initialized for some reason, so set using what is passed in
|
42
|
+
@defaults.each_pair do |k, v|
|
43
|
+
meth = k.to_s
|
44
|
+
rec.send("#{meth[0...-3]}=", v) if meth.end_with? '_id'
|
45
|
+
end
|
46
|
+
# This helps prevent a lookup into the db when we know there couldn't be any data yet
|
47
|
+
@model.reflections.select { |_, v| v.macro == :has_and_belongs_to_many }.keys.each do |r|
|
48
|
+
rec.send("#{r}=", [])
|
49
|
+
end
|
38
50
|
end
|
51
|
+
|
52
|
+
rec
|
39
53
|
else
|
40
54
|
::ActiveRecord::OnDemand::Record.new convert_to_hash(rec), @model, @defaults
|
41
55
|
end
|
data/lib/ar-ondemand/record.rb
CHANGED
@@ -19,15 +19,26 @@ module ActiveRecord
|
|
19
19
|
key = meth.to_s
|
20
20
|
if @record.include? key
|
21
21
|
@changes.include?(key) ? @changes[key] : @record[key]
|
22
|
-
elsif key.end_with?('=') && @record.include?(key[0...-1])
|
22
|
+
elsif key.end_with?('=') && (@record.include?(key[0...-1]) || @record.include?("#{key[0...-1]}_id"))
|
23
23
|
raise ActiveRecord::ReadOnlyRecord if is_readonly?
|
24
24
|
key = key[0...-1]
|
25
25
|
unless key == 'id'
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
val = args[0]
|
27
|
+
passing_in_model = false
|
28
|
+
# Support `model.foo_bar=<instance>` along with `model.foo_bar_id=<int>`
|
29
|
+
if @record.include?("#{key}_id")
|
30
|
+
passing_in_model = true
|
31
|
+
key_model = key
|
32
|
+
key = "#{key}_id"
|
33
|
+
val_model = val
|
34
|
+
val = val.id unless val.nil?
|
35
|
+
end
|
36
|
+
if @record[key] != val
|
37
|
+
return if @record[key] == 1 && val == true
|
38
|
+
return if @record[key] == 0 && val == false
|
39
|
+
return if val.is_a?(Time) && !@record[key].blank? && @record[key].to_time.to_i == val.to_i
|
40
|
+
@changes[key] = val
|
41
|
+
@changes[key_model] = val_model if passing_in_model
|
31
42
|
else
|
32
43
|
# If they changed it, then reverted back, remove from @changes
|
33
44
|
@changes.delete key
|
@@ -54,7 +65,6 @@ module ActiveRecord
|
|
54
65
|
def save
|
55
66
|
raise ActiveRecord::ReadOnlyRecord if is_readonly?
|
56
67
|
return nil if @changes.empty?
|
57
|
-
# logger.debug "Loading model to store changes: #{@changes}"
|
58
68
|
rec = @model.allocate.init_with('attributes' => @record)
|
59
69
|
@changes.each_pair do |key, val|
|
60
70
|
next if key == 'id'
|
data/lib/ar-ondemand/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-ondemand
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Frank
|
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
87
|
version: '0'
|
88
88
|
requirements: []
|
89
89
|
rubyforge_project:
|
90
|
-
rubygems_version: 2.
|
90
|
+
rubygems_version: 2.4.8
|
91
91
|
signing_key:
|
92
92
|
specification_version: 4
|
93
93
|
summary: ActiveRecord On-demand
|