Module: ActiveRecordToGoogleSpreadsheet

Extended by:
Utils
Defined in:
lib/activerecord_to_google_spreadsheet.rb,
lib/activerecord_to_google_spreadsheet/utils.rb,
lib/activerecord_to_google_spreadsheet/converter.rb

Defined Under Namespace

Modules: Converter, Utils Classes: Configuration, GetSessionException

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Utils

get_worksheet_by_name

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



9
10
11
# File 'lib/activerecord_to_google_spreadsheet.rb', line 9

def configuration
  @configuration
end

Class Method Details

.configure {|self.configuration| ... } ⇒ Object

Yields:



20
21
22
23
# File 'lib/activerecord_to_google_spreadsheet.rb', line 20

def self.configure
  self.configuration ||= Configuration.new
  yield self.configuration
end

.dump_to_spreadsheet(session, spreadsheet_key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/activerecord_to_google_spreadsheet.rb', line 40

def self.dump_to_spreadsheet(session, spreadsheet_key)
  self.each_tables(session, spreadsheet_key) do |name, value, ws, clazz, info|

    info[name].each_with_index do |val, index|
      ws[1, index+1] = val
    end

    clazz.all.each_with_index do |val, index|
      clazz.column_names.each_with_index do |col, i|
        ws[index+2, i+1] = val[col]
      end
    end
    ws.save
    ws.reload
  end
end

.each_tables(session, spreadsheet_key) ⇒ Object

class Array

  include Converter::ArrayConverter
end


129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/activerecord_to_google_spreadsheet.rb', line 129

def self.each_tables(session, spreadsheet_key)
  spreadsheet = session.spreadsheet_by_key(spreadsheet_key)
  info = get_db_info
  start_time = Time.now
  info.each do |name, value|
    worksheet = get_worksheet_by_name(spreadsheet, name)
    clazz = name.capitalize.singularize.camelize.to_s.constantize
    yield(name, value, worksheet, clazz, info)
  end
  puts '*************************'
  puts Time.now - start_time
  puts '*************************'
end

.get_authObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/activerecord_to_google_spreadsheet.rb', line 99

def self.get_auth
  client = Google::APIClient.new
  auth = client.authorization
  auth.client_id =  self.configuration.client_id
  auth.client_secret = self.configuration.client_secret
  auth.scope =
    "https://www.googleapis.com/auth/drive " +
    "https://spreadsheets.google.com/feeds/"
  auth.redirect_uri = self.configuration.redirect_uri
  return auth
end

.get_db_infoObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/activerecord_to_google_spreadsheet.rb', line 25

def self.get_db_info
  info = {}
  i = 1
  ActiveRecord::Base.connection.tables.map do |model|
    if i == 1
      i += 1
      next
    end
    clazz = model.capitalize.singularize.camelize.to_s.constantize
    info[model.capitalize.singularize.camelize.to_s] = clazz.column_names
    i += 1
  end
  return info
end

.get_sessionObject



85
86
87
88
89
90
91
# File 'lib/activerecord_to_google_spreadsheet.rb', line 85

def self.get_session
  if $session
    return $session
  else
    raise GetSessionException
  end
end

.google_login_urlObject



111
112
113
114
115
# File 'lib/activerecord_to_google_spreadsheet.rb', line 111

def self.
  auth = get_auth
  auth_url = auth.authorization_uri
  return auth_url.to_s
end

.restore_from_spreadsheet(session, spreadsheet_key) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/activerecord_to_google_spreadsheet.rb', line 57

def self.restore_from_spreadsheet(session, spreadsheet_key)
  self.each_tables(session, spreadsheet_key) do |name, value, ws, clazz, info|
    clazz = name.capitalize.singularize.camelize.to_s.constantize

    (2..ws.num_rows).each do |row|
      c = clazz.find_by_id(ws[row, 1])
      clazz.column_names.each_with_index do |col, i|
        if col == 'id' || col == 'created_at' || col == 'updated_at'
          next
        end
        c[col] = ws[row, i+1]
      end
      c.save
    end
  end
end

.setup_session(code) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/activerecord_to_google_spreadsheet.rb', line 74

def self.setup_session(code)
  if $session
    return $session
  end
  auth = get_auth
  auth.code = code
  auth.fetch_access_token!
  $session = GoogleDrive.(auth.access_token)
  return $session
end