Class: Optimacms::Template

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/optimacms/template.rb

Constant Summary collapse

FORMAT_DEFAULT =
'html'
EXTENSIONS =
{'html'=>'html', 'erb'=>'html.erb', 'haml'=>'html.haml'}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base_dirObject

settings and helpers



163
164
165
# File 'app/models/optimacms/template.rb', line 163

def self.base_dir
  Rails.root.to_s + '/app/views/'
end

.content_filename_ext(format) ⇒ Object



172
173
174
# File 'app/models/optimacms/template.rb', line 172

def self.content_filename_ext(format)
  EXTENSIONS[format]
end

.content_filename_lang_postfix(lang) ⇒ Object



167
168
169
170
# File 'app/models/optimacms/template.rb', line 167

def self.content_filename_lang_postfix(lang)
  return '' if lang==''
  return '.'+lang
end

.create_folders_tree(dirpath) ⇒ Object

create folders if necessary - NOT WORKING



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'app/models/optimacms/template.rb', line 304

def self.create_folders_tree(dirpath)
  a = dirpath.split /\//

  d = ''
  parent_current = nil
  prev_d = d
  a.each do |s|
    d = d + (d=='' ? '' : '/') +s
    r = Template.where(is_folder: true, basepath: d).first
    if r
      parent_current = r
      next
    end
    # create missing folder

    path_new = File.basename(d)
    basedirpath_new = prev_d
    r = Template.create(is_folder: true, basename: path_new, basedirpath: basedirpath_new, title: path_new, parent: parent_current)
    parent_current = r

    prev_d = d
  end

  parent_current

end

.where_parent(parent_id) ⇒ Object

search



68
69
70
71
72
73
74
# File 'app/models/optimacms/template.rb', line 68

def self.where_parent(parent_id)
  if parent_id==0
    roots
  elsif parent_id>0
    children_of(parent_id.to_s)
  end
end

Instance Method Details

#_before_createObject



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'app/models/optimacms/template.rb', line 238

def _before_create
  if self.is_folder
    # create dir on disk

    Fileutils::Fileutils.create_dir_if_not_exists self.folder_fullpath
  else
    # add folders if needed

    if self.parent_id.nil? && !self.basedirpath.blank?
      self.parent = Template.create_folders_tree(self.basedirpath)
    end

  end

end

#_before_destroyObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'app/models/optimacms/template.rb', line 281

def _before_destroy
  if self.is_folder

    # cannot delete not empty dir

    if !self.folder_empty?
      return false
    end

    # delete dir

    folder_delete

  else
    # file



  end


  true
end

#_before_updateObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'app/models/optimacms/template.rb', line 252

def _before_update
  if self.is_folder
    oldpath = self.folder_fullpath

    self.make_basepath

    # rename dir on disk

    if self.basename_changed?
      require 'fileutils'
      newpath = self.folder_fullpath

      if Dir.exists?(newpath)
        errors.add(:basename, 'Folder already exists')
        return false
      end

      FileUtils.mv oldpath, newpath
    end

  else
    # file


    # TODO: rename file

    #self.make_basepath


  end

end

#_before_validationObject

callbacks



224
225
226
227
228
229
230
231
232
233
234
235
# File 'app/models/optimacms/template.rb', line 224

def _before_validation
  fix_basedirpath

  # parent, basedirpath

  if self.parent_id.nil? && !self.basedirpath.blank?
    set_parent_from_basedirpath
  elsif self.basedirpath.nil? && !self.parent_id.nil?
    set_basedirpath_from_parent
  end

  set_basepath
end

#attachObject

operations



80
81
82
83
84
85
86
87
88
# File 'app/models/optimacms/template.rb', line 80

def attach
  # check if file exists

  if !content_file_exists?
    errors.add(:attach, "file not found")
    return false
  end

  save
end

#check_parent_folder_existsObject

validations



121
122
123
124
125
# File 'app/models/optimacms/template.rb', line 121

def check_parent_folder_exists
  if self.parent_id.nil? && !self.basedirpath.blank?
    errors.add(:basedirpath, "folder not exists. create it first")
  end
end

#content(lang = '') ⇒ Object

content



147
148
149
150
151
152
# File 'app/models/optimacms/template.rb', line 147

def content(lang='')
  filename =  fullpath(lang)
  return nil if filename.nil?
  return '' if !File.exists? filename
  File.read(filename)
end

#content=(v, lang = '') ⇒ Object



154
155
156
157
158
# File 'app/models/optimacms/template.rb', line 154

def content=(v, lang='')
  File.open(self.fullpath(lang), "w+") do |f|
    f.write(v)
  end
end

#content_file_exists?(lang = '') ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/optimacms/template.rb', line 105

def content_file_exists?(lang='')
  postfixes = EXTENSIONS.map{|k,v| '.'+v}

  found = false
  postfixes.each do |p|
    if File.exists?(self.fullpath(lang))
      found = true
      break
    end
  end
  found
end

#fix_basedirpathObject



190
191
192
193
194
195
# File 'app/models/optimacms/template.rb', line 190

def fix_basedirpath
  self.basedirpath ||= ''
  if !self.basedirpath.blank? && self.basedirpath[-1] !='/'
    self.basedirpath << '/'
  end
end

#folder_deleteObject

folders



333
334
335
336
# File 'app/models/optimacms/template.rb', line 333

def folder_delete
  dirpath = self.folder_fullpath
  FileUtils.remove_dir(dirpath) if Dir.exists? dirpath
end

#folder_empty?Boolean

Returns:

  • (Boolean)


338
339
340
341
342
343
344
345
# File 'app/models/optimacms/template.rb', line 338

def folder_empty?
  dirpath = self.folder_fullpath
  if !Dir.exists? dirpath
    return true
  end

  return Dir.entries(dirpath).size == 2
end

#folder_fullpathObject



137
138
139
140
141
# File 'app/models/optimacms/template.rb', line 137

def folder_fullpath
  f = self.folder_path
  return nil if f.nil?
  Template.base_dir + f
end

#folder_pathObject

folder



132
133
134
135
# File 'app/models/optimacms/template.rb', line 132

def folder_path
  return nil if self.basepath.nil?
  self.basepath+'/'
end

#fullpath(lang = '') ⇒ Object



99
100
101
102
103
# File 'app/models/optimacms/template.rb', line 99

def fullpath(lang='')
  f = self.path(lang)
  return nil if f.nil?
  Template.base_dir + f
end

#has_code?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/optimacms/template.rb', line 57

def has_code?
  ['haml', 'erb'].include?(self.tpl_format)
end

#is_folder?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/optimacms/template.rb', line 61

def is_folder?
  self.is_folder
end

#parent_titleObject

properties



52
53
54
# File 'app/models/optimacms/template.rb', line 52

def parent_title
  self.parent.title
end

#path(lang = '') ⇒ Object

path



94
95
96
97
# File 'app/models/optimacms/template.rb', line 94

def path(lang='')
  return nil if self.basename.nil?
  self.basedirpath+self.basename+Template.content_filename_lang_postfix(lang)+'.'+Template.content_filename_ext(self.tpl_format)
end

#set_basedirpath_from_parentObject



197
198
199
200
201
202
203
204
205
206
207
# File 'app/models/optimacms/template.rb', line 197

def set_basedirpath_from_parent
  v = ''
  if self.parent_id.nil?
    v = ''
  else
    v = self.parent.basepath+'/' if self.parent_id >0
  end

  self.basedirpath = v
  #v

end

#set_basepathObject



178
179
180
181
182
183
184
185
186
187
# File 'app/models/optimacms/template.rb', line 178

def set_basepath
  if self.parent.nil?
    self.basepath = self.basename
    self.basedirpath ||= ''
  else
    self.basepath = self.parent.basepath+'/'+self.basename
    self.basedirpath ||= self.parent.basepath+'/'
  end

end

#set_parent_from_basedirpathObject



209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/optimacms/template.rb', line 209

def set_parent_from_basedirpath
  v = self.basedirpath

  # set parent_id from dir

  d = v.chop
  r = Template.where(is_folder: true, basepath: d).first
  if r
    self.parent_id = r.id
  else
    self.parent_id = nil
  end
end

#to_sObject



45
46
47
# File 'app/models/optimacms/template.rb', line 45

def to_s
  "#{title} (#{basepath})"
end