activeresource-google_spreadsheets 0.0.4 → 0.0.5
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee5a88e671575377719e758fbb016246e7a57442
|
4
|
+
data.tar.gz: 9a23675af46c1f18dc10a790ace9ee1cf131bede
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e9067064a7a0d96b896d5baf966aee497ed628c3b4fa9e37ecfad81c80a2e5a25afdf7fa72be15c9ae16d1215c896d1d5a943119250d2e7a0a4dce37340892de
|
7
|
+
data.tar.gz: 1e2f0d7ed3ab42e91028b858583fb4e38fab015d662b5b9622b7ca054db123dc763643b34dd010f67d627f93e2c79058e1ad3d23ea0ea1a82983ba98133b2be0
|
@@ -3,6 +3,9 @@ module GoogleSpreadsheets
|
|
3
3
|
module Syncing
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
|
+
REL_NAME_SPREADSHEET = 'http://schemas.google.com/spreadsheets/2006#spreadsheet'
|
7
|
+
REL_NAME_ROW = 'http://schemas.google.com/spreadsheets/2006#listfeed'
|
8
|
+
|
6
9
|
included do
|
7
10
|
class_attribute :synchronizers
|
8
11
|
self.synchronizers = {}
|
@@ -18,7 +21,8 @@ module GoogleSpreadsheets
|
|
18
21
|
def sync_with(rows_name, options)
|
19
22
|
options.assert_valid_keys(:spreadsheet_id, :worksheet_title, :class_name)
|
20
23
|
options[:worksheet_title] ||= rows_name.to_s
|
21
|
-
|
24
|
+
options[:class_name] ||= rows_name.to_s.classify
|
25
|
+
synchronizer = Synchronizer.new(self, options[:class_name].safe_constantize, options[:spreadsheet_id], options[:worksheet_title])
|
22
26
|
self.synchronizers.merge!(rows_name => synchronizer)
|
23
27
|
|
24
28
|
# rows accessor
|
@@ -42,29 +46,37 @@ module GoogleSpreadsheets
|
|
42
46
|
end
|
43
47
|
|
44
48
|
class Synchronizer
|
45
|
-
attr_reader :
|
49
|
+
attr_reader :record_class, :row_class, :spreadsheet_id, :worksheet_title
|
46
50
|
|
47
|
-
def initialize(
|
48
|
-
@
|
51
|
+
def initialize(record_class, row_class, spreadsheet_id, worksheet_title)
|
52
|
+
@record_class = record_class
|
53
|
+
@row_class = row_class || default_class_for(REL_NAME_ROW)
|
49
54
|
@spreadsheet_id = spreadsheet_id
|
50
55
|
@worksheet_title = worksheet_title
|
51
56
|
end
|
52
57
|
|
53
58
|
def all_rows
|
54
|
-
worksheet.
|
59
|
+
reflections = worksheet.class.reflections.values.find_all{|ref| ref.is_a?(LinkRelations::LinkRelationReflection) }
|
60
|
+
if reflection = reflections.find{|ref| ref.klass == row_class }
|
61
|
+
worksheet.send(reflection.name)
|
62
|
+
elsif reflection = reflections.find{|ref| ref.options[:rel] == REL_NAME_ROW }
|
63
|
+
worksheet.send(reflection.name, as: row_class.to_s)
|
64
|
+
else
|
65
|
+
raise "Reflection for #{row_class.to_s} not found."
|
66
|
+
end
|
55
67
|
end
|
56
68
|
|
57
69
|
def sync_with_rows
|
58
70
|
reset
|
59
71
|
records = all_rows.map do |row|
|
60
|
-
|
72
|
+
record_class.find_or_initialize_by(id: row.id).tap do |record|
|
61
73
|
row.aliased_attributes.each do |attr|
|
62
74
|
record.send("#{attr}=", row.send(attr))
|
63
75
|
end
|
64
76
|
end
|
65
77
|
end
|
66
78
|
skipping_outbound_sync_of(records) do |records_with_skipped_outbound|
|
67
|
-
transaction_if_possible(
|
79
|
+
transaction_if_possible(record_class) do
|
68
80
|
records_with_skipped_outbound.each(&:save)
|
69
81
|
end
|
70
82
|
end
|
@@ -88,10 +100,10 @@ module GoogleSpreadsheets
|
|
88
100
|
end
|
89
101
|
|
90
102
|
def worksheet
|
91
|
-
@worksheet ||=
|
92
|
-
.find(
|
103
|
+
@worksheet ||= default_class_for(REL_NAME_SPREADSHEET)
|
104
|
+
.find(spreadsheet_id)
|
93
105
|
.worksheets
|
94
|
-
.find_by!(title:
|
106
|
+
.find_by!(title: worksheet_title)
|
95
107
|
end
|
96
108
|
|
97
109
|
def reset
|
@@ -100,8 +112,8 @@ module GoogleSpreadsheets
|
|
100
112
|
end
|
101
113
|
|
102
114
|
private
|
103
|
-
def
|
104
|
-
LinkRelations.class_name_mappings[rel_name].classify
|
115
|
+
def default_class_for(rel_name)
|
116
|
+
LinkRelations.class_name_mappings[rel_name].classify.constantize
|
105
117
|
end
|
106
118
|
|
107
119
|
def transaction_if_possible(origin = self, &block)
|
@@ -36,15 +36,17 @@ module GoogleSpreadsheets
|
|
36
36
|
def defines_links_to_many_finder_method(method_name, relation_name, association_model)
|
37
37
|
ivar_name = :"@#{method_name}"
|
38
38
|
|
39
|
-
define_method(method_name) do
|
39
|
+
define_method(method_name) do |options = {}|
|
40
|
+
options.assert_valid_keys(:as)
|
40
41
|
if instance_variable_defined?(ivar_name)
|
41
42
|
instance_variable_get(ivar_name)
|
42
43
|
elsif attributes.include?(method_name)
|
43
44
|
attributes[method_name]
|
44
45
|
else
|
45
|
-
|
46
|
+
model = options[:as].try{|as| as.to_s.safe_constantize } || association_model
|
47
|
+
link = self.link.find{|l| l.rel == relation_name }
|
46
48
|
path = link.href.slice(%r|^https://spreadsheets\.google\.com(/.*)|, 1)
|
47
|
-
instance_variable_set(ivar_name,
|
49
|
+
instance_variable_set(ivar_name, model.find(:all, from: path))
|
48
50
|
end
|
49
51
|
end
|
50
52
|
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.0.
|
4
|
+
version: 0.0.5
|
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: 2014-01-
|
12
|
+
date: 2014-01-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activeresource
|