Class: Optimacms::Template
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Optimacms::Template
- Defined in:
- app/models/optimacms/template.rb
Constant Summary collapse
- EXTENSION_DEFAULT =
'html'- EXTENSIONS =
{'blank'=>'', 'html'=>'html', 'erb'=>'html.erb', 'haml'=>'html.haml'}
Class Method Summary collapse
-
.base_dir ⇒ Object
settings.
-
.create_folders_tree(dirpath) ⇒ Object
create folders if necessary - NOT WORKING.
- .filename_ext(format) ⇒ Object
- .filename_ext_with_dot(format) ⇒ Object
- .filename_lang_postfix(lang) ⇒ Object
-
.where_parent(parent_id) ⇒ Object
search.
Instance Method Summary collapse
- #_before_create ⇒ Object
- #_before_destroy ⇒ Object
- #_before_update ⇒ Object
-
#_before_validation ⇒ Object
callbacks.
-
#attach ⇒ Object
operations.
-
#build_translations ⇒ Object
translations.
-
#check_parent_folder_exists ⇒ Object
validations.
-
#content(lang = '') ⇒ Object
content.
- #content=(v, lang = '') ⇒ Object
- #content_file_exists?(lang = '') ⇒ Boolean
-
#filename_base ⇒ Object
base filename depending on type for page = name for partial = _name.
- #fix_basedirpath ⇒ Object
-
#folder_delete ⇒ Object
folders.
- #folder_empty? ⇒ Boolean
- #folder_fullpath ⇒ Object
-
#folder_path ⇒ Object
folder.
- #fullpath(lang = '') ⇒ Object
- #has_code? ⇒ Boolean
- #is_folder? ⇒ Boolean
- #is_layout? ⇒ Boolean
- #is_type_page? ⇒ Boolean
- #is_type_partial? ⇒ Boolean
-
#parent_title ⇒ Object
properties.
-
#path(lang = '') ⇒ Object
path.
- #set_basedirpath_from_parent ⇒ Object
-
#set_basepath ⇒ Object
operations with path.
- #set_parent_from_basedirpath ⇒ Object
- #to_s ⇒ Object
Class Method Details
.base_dir ⇒ Object
settings
53 54 55 |
# File 'app/models/optimacms/template.rb', line 53 def self.base_dir Rails.root.to_s + '/app/views/' end |
.create_folders_tree(dirpath) ⇒ Object
create folders if necessary - NOT WORKING
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'app/models/optimacms/template.rb', line 369 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 |
.filename_ext(format) ⇒ Object
223 224 225 |
# File 'app/models/optimacms/template.rb', line 223 def self.filename_ext(format) EXTENSIONS[format] end |
.filename_ext_with_dot(format) ⇒ Object
227 228 229 230 231 |
# File 'app/models/optimacms/template.rb', line 227 def self.filename_ext_with_dot(format) ext = filename_ext(format) ext = '.'+ext unless ext.blank? ext end |
.filename_lang_postfix(lang) ⇒ Object
217 218 219 220 221 |
# File 'app/models/optimacms/template.rb', line 217 def self.filename_lang_postfix(lang) lang = lang.to_s return '' if lang=='' return '.'+lang end |
.where_parent(parent_id) ⇒ Object
search
112 113 114 115 116 117 118 |
# File 'app/models/optimacms/template.rb', line 112 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_create ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'app/models/optimacms/template.rb', line 298 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_destroy ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'app/models/optimacms/template.rb', line 346 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_update ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'app/models/optimacms/template.rb', line 312 def _before_update if self.is_folder oldpath = self.folder_fullpath self.set_basepath # rename dir on disk if self.basename_changed? raise "cannot change folder path" require 'fileutils' newpath = self.folder_fullpath if Dir.exists?(newpath) errors.add(:basename, 'Folder already exists') return false end FileUtils.mv oldpath, newpath # TODO: change paths for child templates end else # file # TODO: rename file #self.make_basepath end end |
#_before_validation ⇒ Object
callbacks
284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'app/models/optimacms/template.rb', line 284 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 |
#attach ⇒ Object
operations
124 125 126 127 128 129 130 131 132 |
# File 'app/models/optimacms/template.rb', line 124 def attach # check if file exists if !content_file_exists? errors.add(:attach, "file not found") return false end save end |
#build_translations ⇒ Object
translations
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/models/optimacms/template.rb', line 92 def build_translations # if is_translated langs = Language.list_with_default else langs = [''] end # langs_missing = langs - self.translations.all.map{|r| r.lang} langs_missing.each do |lang| self.translations.new(:lang=>lang) end end |
#check_parent_folder_exists ⇒ Object
validations
165 166 167 168 169 |
# File 'app/models/optimacms/template.rb', line 165 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
191 192 193 194 195 196 |
# File 'app/models/optimacms/template.rb', line 191 def content(lang='') filename = fullpath(lang) return nil if filename.nil? return '' if !File.exists? filename File.read(filename) end |
#content=(v, lang = '') ⇒ Object
198 199 200 201 202 |
# File 'app/models/optimacms/template.rb', line 198 def content=(v, lang='') File.open(self.fullpath(lang), "w+") do |f| f.write(v) end end |
#content_file_exists?(lang = '') ⇒ Boolean
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'app/models/optimacms/template.rb', line 149 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 |
#filename_base ⇒ Object
base filename depending on type for page = name for partial = _name
210 211 212 213 214 |
# File 'app/models/optimacms/template.rb', line 210 def filename_base return '_'+self.basename if self.is_type_partial? self.basename end |
#fix_basedirpath ⇒ Object
248 249 250 251 252 253 |
# File 'app/models/optimacms/template.rb', line 248 def fix_basedirpath self.basedirpath ||= '' if !self.basedirpath.blank? && self.basedirpath[-1] !='/' self.basedirpath << '/' end end |
#folder_delete ⇒ Object
folders
398 399 400 401 |
# File 'app/models/optimacms/template.rb', line 398 def folder_delete dirpath = self.folder_fullpath FileUtils.remove_dir(dirpath) if Dir.exists? dirpath end |
#folder_empty? ⇒ Boolean
403 404 405 406 407 408 409 410 |
# File 'app/models/optimacms/template.rb', line 403 def folder_empty? dirpath = self.folder_fullpath if !Dir.exists? dirpath return true end return Dir.entries(dirpath).size == 2 end |
#folder_fullpath ⇒ Object
181 182 183 184 185 |
# File 'app/models/optimacms/template.rb', line 181 def folder_fullpath f = self.folder_path return nil if f.nil? Template.base_dir + f end |
#folder_path ⇒ Object
folder
176 177 178 179 |
# File 'app/models/optimacms/template.rb', line 176 def folder_path return nil if self.basepath.nil? self.basepath+'/' end |
#fullpath(lang = '') ⇒ Object
143 144 145 146 147 |
# File 'app/models/optimacms/template.rb', line 143 def fullpath(lang='') f = self.path(lang) return nil if f.nil? Template.base_dir + f end |
#has_code? ⇒ Boolean
68 69 70 |
# File 'app/models/optimacms/template.rb', line 68 def has_code? ['haml', 'erb', 'blank'].include?(self.tpl_format) end |
#is_folder? ⇒ Boolean
72 73 74 |
# File 'app/models/optimacms/template.rb', line 72 def is_folder? self.is_folder end |
#is_layout? ⇒ Boolean
76 77 78 |
# File 'app/models/optimacms/template.rb', line 76 def is_layout? return self.type_id==TemplateType::TYPE_LAYOUT end |
#is_type_page? ⇒ Boolean
80 81 82 |
# File 'app/models/optimacms/template.rb', line 80 def is_type_page? return self.type_id==TemplateType::TYPE_PAGE end |
#is_type_partial? ⇒ Boolean
84 85 86 |
# File 'app/models/optimacms/template.rb', line 84 def is_type_partial? return self.type_id==TemplateType::TYPE_PARTIAL end |
#parent_title ⇒ Object
properties
63 64 65 |
# File 'app/models/optimacms/template.rb', line 63 def parent_title self.parent.title end |
#path(lang = '') ⇒ Object
path
138 139 140 141 |
# File 'app/models/optimacms/template.rb', line 138 def path(lang='') return nil if self.basename.nil? self.basedirpath + filename_base + Template.filename_lang_postfix(lang) + Template.filename_ext_with_dot(self.tpl_format) end |
#set_basedirpath_from_parent ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 |
# File 'app/models/optimacms/template.rb', line 255 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_basepath ⇒ Object
operations with path
236 237 238 239 240 241 242 243 244 245 |
# File 'app/models/optimacms/template.rb', line 236 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_basedirpath ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'app/models/optimacms/template.rb', line 267 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_s ⇒ Object
46 47 48 |
# File 'app/models/optimacms/template.rb', line 46 def to_s "#{title} (#{basepath})" end |