Class: Optimacms::BackupMetadata::TemplateImport

Inherits:
Object
  • Object
show all
Defined in:
lib/optimacms/backup_metadata/template_import.rb

Class Method Summary collapse

Class Method Details

.analyze_data_dir(dir_path) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/optimacms/backup_metadata/template_import.rb', line 6

def self.analyze_data_dir(dir_path)
  #
  files = []

  #Dir["#{dir_path}/templates/*.json"].each do |f|
  Dir["#{dir_path}/*.json"].each do |f|
    files << f
  end

  #files_templates = files_templates.sort_by{ |x| File.mtime(x) }.reverse
  files = files.sort_by{ |x| File.basename(x, '.json') }

  #
  res = files.map do |f|
    data = load_template_data(f)
    analysis = analyze_template_data(data)

    {filename: File.basename(f), data: data, analysis: analysis}
  end


  res
end

.analyze_template_data(data) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/optimacms/backup_metadata/template_import.rb', line 41

def self.analyze_template_data(data)
  tpl_basepath = data['basepath']

  #
  res = {}
  res['status'] = ''
  res['basepath'] = tpl_basepath
  res['changes'] = []
  res['errors'] = []
  res['warnings'] = []


  # find in db
  row = Template.where(basepath: tpl_basepath).first

  # statuses: new, not_compatible, changed

  if row.nil?
    res['status'] = 'new'

    # folder
    if data['is_folder']

    else


    end

    # check parents
    if data['ancestry_path'] && data['ancestry_path']!=""
      parents = data['ancestry_path'].split /,/

      current_parent = nil
      parents.each do |s_parent|

        if current_parent.nil?
          row_parent = Template.roots.where(basename: s_parent).first
        else
          row_parent = Template.children_of(current_parent).where(basename: s_parent).first
        end

        #
        if row_parent.nil?
          res['errors'] << {field: 'parent', message: "parent not found: #{s_parent}"}

          res['status'] = 'error'
          break
        end

        # next step
        current_parent = row_parent


      end
    end

  else
    # compare fields
    basic_fields = ['title', 'tpl_format', 'is_translated']

    basic_fields.each do |field|
      if data[field]!=row.send(field.to_sym)
        res['changes'] << {field: field}
      end
    end

    # id
    if row.id != data['id'].to_i
      res['warnings'] << {field: 'id', message: "id changed"}
    end

    # folder
    if row.is_folder != data['is_folder']
      res['changes'] << {field: 'is_folder', message: "is folder error"}
    end

    # type
    row_type_name = (row.type.name rescue nil)
    #if (row.type_id.nil? && !data['type_id'].nil?) || (!row.type_id.nil? && data['type_id'].nil?)
    #  res['changes'] << {field: 'type'}
    #elsif row.type.name != data['type_name']
    #  res['changes'] << {field: 'type'}
    #end

    if row_type_name != data['type_name']
      res['changes'] << {field: 'type'}
    end

    # parent
    row_ancestry_path = (row.ancestors.map{|r| r.basepath}.join(",") rescue nil)

    if (row_ancestry_path.nil? && !data['ancestry_path'].nil?) || (!row_ancestry_path.nil? && data['ancestry_path'].nil?)
      res['changes'] << {field: 'parent'}
    else
      if row_ancestry_path != data['ancestry_path']
        res['changes'] << {field: 'parent'}
      end

    end

    #
    if !res['changes'].empty?
      res['status'] = 'changed'
    end

  end

  res
end

.analyze_template_file(filename) ⇒ Object


31
32
33
34
35
# File 'lib/optimacms/backup_metadata/template_import.rb', line 31

def self.analyze_template_file(filename)
  data = load_template_data(filename)

  return analyze_template_data(data)
end

.dir_backupsObject

settings


152
153
154
# File 'lib/optimacms/backup_metadata/template_import.rb', line 152

def self.dir_backups
  BackupMetadata::Backup.dir_backups
end

.import_template(backup_dir, filename, cmd) ⇒ Object

import


157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/optimacms/backup_metadata/template_import.rb', line 157

def self.import_template(backup_dir, filename, cmd)
  dir_path = File.join(dir_backups, backup_dir, "templates")

  if cmd=='add'
    return import_template_add(dir_path, filename)
  elsif cmd=='update'
    return import_template_update(dir_path, filename)
  else
    raise 'unknown command'
  end

rescue =>e
  return {res: 0, message: e.message}
end

.import_template_add(dir_path, filename) ⇒ Object


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/optimacms/backup_metadata/template_import.rb', line 172

def self.import_template_add(dir_path, filename)
  # init
  f = File.join(dir_path, filename)

  data = load_template_data(f)
  analysis = analyze_template_data(data)

  # precheck
  if analysis['status']!='new'
    raise 'Template is not NEW'
  end

  # insert to db
  row=Template.new(basepath: data['basepath'])
  init_template_from_data row, data

  row.save!



  res = {res: 1, message: "ok"}
  return res

rescue => e
  res = {res: 0, message: e.message}
  return res
end

.import_template_update(dir_path, filename) ⇒ Object


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/optimacms/backup_metadata/template_import.rb', line 200

def self.import_template_update(dir_path, filename)
  # init
  f = File.join(dir_path, filename)

  data = load_template_data(f)
  analysis = analyze_template_data(data)

  # precheck
  if analysis['status']!='changed'
    raise 'Template is not CHANGED'
  end

  # update in db
  row = Template.where(basepath: data['basepath']).first
  init_template_from_data row, data
  row.save!



  res = {res: 1, message: "ok"}
  return res

rescue => e
  res = {res: 0, message: e.message}
  return res
end

.init_template_from_data(row, data) ⇒ Object


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/optimacms/backup_metadata/template_import.rb', line 229

def self.init_template_from_data(row, data)
  #
  #{"title"=>"t5", "basedirpath"=>"temp/", "basename"=>"t5",
  # "type_id"=>"2", "tpl_format"=>"haml", "is_translated"=>"0"}

  ['title', 'basedirpath', 'basename', "tpl_format"].each do |f|
    row.send("#{f}=", data[f])
  end

  row.is_folder = data['is_folder'] ? 1 : 0
  row.is_translated = data['is_translated'] ? 1 : 0

  # type_id
  row.type_id = TemplateType.get_id_by_name(data['type_name'])


  row
end

.load_template_data(filename) ⇒ Object


37
38
39
# File 'lib/optimacms/backup_metadata/template_import.rb', line 37

def self.load_template_data(filename)
  data = JSON.parse(File.read(filename))
end