activeresource-google_spreadsheets 0.1.6 → 0.1.7

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
  SHA1:
3
- metadata.gz: 4c916e1df6f4a1dcd1ce7336b1db3fbb3c5d519a
4
- data.tar.gz: 7488857b14462153ebf1e0141fea10f677b8370f
3
+ metadata.gz: 2c5bcdb99d1d9f0f957e277e28b1ad099c29fd8e
4
+ data.tar.gz: 0a95a492fa7435a2c50e0b10ed041c07e9f1a351
5
5
  SHA512:
6
- metadata.gz: d8dfaa0b5c4bfe194aa4bf856c9a362373f5628e02c2f3ec716defcd694c21360b4c81e9ac8ccff9b963bbf4f4c1e959498fb6c015d39d1678da2df9f3255330
7
- data.tar.gz: 9277e3fd7ad37c5e3139ac5e92d3490865825d3e698cd8c7f779d2efc0035ad8c3b055a9d6cb537cb2f24bd0c0c64b860f98298509f259715bcd44c01cee7d08
6
+ metadata.gz: da3e9aaa716cc7a9a40f1f9955f0d539679e60b7980f37ac8db00fe41be2860bd2aa7eddd41d8e9d381cf7fd80083da0d735a0d8ffa545ff7fd0ea2796057f93
7
+ data.tar.gz: c84889b8eae583ac50d6b1d5e8390bdbbcf8a01308302bb9b0624ba27aef59dee017892aa2af093f48ca54b72104d4d0df0474f8b87edf14733e4501a116e42f
@@ -24,9 +24,13 @@ module GoogleSpreadsheets
24
24
  end
25
25
 
26
26
  def where(condition_hash)
27
- condition_hash.inject(self.to_a) do |array, (attr, value)|
27
+ conditioned_elements = condition_hash.inject(self.to_a) do |array, (attr, value)|
28
28
  array.find_all{|element| element.send(attr) == value }
29
29
  end
30
+ self.class.new(conditioned_elements).tap do |collection|
31
+ collection.resource_class = self.resource_class
32
+ collection.original_params = self.original_params
33
+ end
30
34
  end
31
35
  end
32
36
  end
@@ -18,12 +18,13 @@ module GoogleSpreadsheets
18
18
  # sync_with :user_rows, spreadsheet_id: 'xxxx',
19
19
  # worksheet_title: 'users'
20
20
  # after_commit :sync_user_row
21
- def sync_with(rows_name, options)
21
+ def sync_with(rows_name, scope = nil, options)
22
22
  options.assert_valid_keys(:spreadsheet_id, :worksheet_title, :class_name, :assigner, :include_blank, :ignore_blank_id)
23
23
  opts = options.dup
24
24
  spreadsheet_id = opts.delete(:spreadsheet_id)
25
25
  worksheet_title = opts.delete(:worksheet_title) || rows_name.to_s
26
26
  class_name = opts.delete(:class_name) || rows_name.to_s.classify
27
+ opts[:scope] = scope
27
28
  synchronizer = Synchronizer.new(self, class_name.safe_constantize, spreadsheet_id, worksheet_title, opts)
28
29
  self.synchronizers = self.synchronizers.merge(rows_name => synchronizer) # not share parent class attrs
29
30
 
@@ -60,19 +61,26 @@ module GoogleSpreadsheets
60
61
 
61
62
  def all_rows
62
63
  reflections = worksheet.class.reflections.values.find_all{|ref| ref.is_a?(LinkRelations::LinkRelationReflection) }
63
- if reflection = reflections.find{|ref| ref.klass == row_class }
64
- worksheet.send(reflection.name)
65
- elsif reflection = reflections.find{|ref| ref.options[:rel] == REL_NAME_ROW }
66
- worksheet.send(reflection.name, as: row_class.to_s)
67
- else
68
- raise "Reflection for #{row_class.to_s} not found."
64
+ collection =
65
+ if reflection = reflections.find{|ref| ref.klass == row_class }
66
+ worksheet.send(reflection.name)
67
+ elsif reflection = reflections.find{|ref| ref.options[:rel] == REL_NAME_ROW }
68
+ worksheet.send(reflection.name, as: row_class.to_s)
69
+ else
70
+ raise "Reflection for #{row_class.to_s} not found."
71
+ end
72
+ if @options[:scope]
73
+ collection = collection.instance_exec(&@options[:scope])
69
74
  end
75
+ collection
70
76
  end
71
77
 
72
- def sync_with_rows
78
+ def sync_with_rows(scope = nil)
73
79
  reset
74
80
  records_to_save = {}
75
- all_rows.each do |row|
81
+ rows = all_rows
82
+ rows = rows.instance_exec(&scope) if scope
83
+ rows.each do |row|
76
84
  if row.id.present?
77
85
  record_id = row.id.to_i
78
86
  record = records_to_save[record_id] || record_class.find_or_initialize_by(id: record_id)
@@ -1,3 +1,3 @@
1
1
  module GoogleSpreadsheets
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeresource-google_spreadsheets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chihiro Ito
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-03-02 00:00:00.000000000 Z
12
+ date: 2017-04-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activeresource
@@ -140,3 +140,4 @@ signing_key:
140
140
  specification_version: 4
141
141
  summary: Google Spreadsheets accessor with ActiveResource
142
142
  test_files: []
143
+ has_rdoc: