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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzFmMGZjYTQyODc2ZDY5ZGFiNzUxYTYyYTVlYzc4ZGZhNGFiMjIwZg==
4
+ Yzk3YzYxMjU4NmM2MzQ2MDczNWRmM2JjMDkwYzQ1ZTQ3M2E4OWMzMg==
5
5
  data.tar.gz: !binary |-
6
- MDc4YWY1NjE1YzdhMjM0Njg4NjEyNjgzN2VhNzg5ODdiN2JlZDIxYg==
6
+ OGQyZDMxNDY3Zjg1YWVmMDcwNDgwNzYwOWQ0NTk0MjkyOTM0MjQ0Ng==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDFhMTAxMWY5OTc5OTEwODRiMzY2MjI1MTg2Y2FhOWZjODRhMTJiOGU0ZjRi
10
- MTgzOGM4OTQzOTBkNjM4ZTM3ZDdlZDk2NWYwYjA0NTY0ZjMzNDE0NjY5MzA4
11
- NDJlNDAwOTU2YjA5OWEyNTc3MWU1MGZkYjljNjk4YmRmZDBiMGM=
9
+ NmY1YzAzZjI4MDY5MThhMjc5NjVjZmFiOTE0YjIyMTVlNjE0ZTRhZmM5ZDFi
10
+ MDVjMGIzNDRkZDJhMjkxMjcwNzY2NjI0MTg1MjBkZDIxZTcxYjJhOWE5ZDcx
11
+ Y2IxZmFmZDc3NDQ0YWVjYWZkM2E1ZjNmMjU4NThlYjdhNTA2MjM=
12
12
  data.tar.gz: !binary |-
13
- NzM3YWMyMDM5ZjdiM2Q1ZjhjZjMwMDk2MmJmY2I0ZTkzNTZkZjcxODYzOGFk
14
- MjAzZDk2MjFlZWQyMWFkNTc0ZmY2NmM2ZWIzOTI3ZjY3YzJjZTU4ZjQwOWYz
15
- ZmI3YTA2OTcyODI1ZmEzZjgyNTM0NjlhMzE2ZDgxMzI4NjY1NGE=
13
+ ZGM5NzJhYTlhZDg0NGJhNWNjNGRmYzQ2YTc5NTc3Y2ZkYmE4NWI5MTQ0OGE4
14
+ MThhZTk1NzgyYjc1Njc5OTI5YTA0MjhkODQ0YTAyYzJjOTRlNTA4YWFjYTU0
15
+ N2E1OWYwM2IyY2FmMTZiZTdhNzkyNWQ5OGQyN2M0OTM0ZTE3ZjQ=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ar-ondemand (1.0.5)
4
+ ar-ondemand (1.1.0)
5
5
  activerecord (>= 3.2)
6
6
  activesupport (>= 3.2)
7
7
 
@@ -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
- return query(self) if options.empty?
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 "Key column cannot be blank." if key_column.blank?
14
- raise "Defaults cannot be empty." if defaults.empty?
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 "Search key cannot be blank." if key.blank?
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
- 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
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
@@ -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
- if @record[key] != args[0]
27
- return if @record[key] == 1 && args[0] == true
28
- return if @record[key] == 0 && args[0] == false
29
- return if args[0].is_a?(Time) && !@record[key].blank? && @record[key].to_time.to_i == args[0].to_i
30
- @changes[key] = args[0]
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'
@@ -1,3 +1,3 @@
1
1
  module ArOnDemand
2
- VERSION = '1.0.5'
2
+ VERSION = '1.1.0'
3
3
  end
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.5
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.3.0
90
+ rubygems_version: 2.4.8
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: ActiveRecord On-demand