Class: Optimacms::Template
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Optimacms::Template
- 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
-
.base_dir ⇒ Object
settings and helpers.
- .content_filename_ext(format) ⇒ Object
- .content_filename_lang_postfix(lang) ⇒ Object
-
.create_folders_tree(dirpath) ⇒ Object
create folders if necessary - NOT WORKING.
- .get_where(values) ⇒ Object
-
.list(filter, pg) ⇒ Object
search.
Instance Method Summary collapse
- #_before_create ⇒ Object
- #_before_destroy ⇒ Object
- #_before_update ⇒ Object
-
#_before_validation ⇒ Object
callbacks.
- #attach ⇒ Object
-
#check_parent_folder_exists ⇒ Object
validations.
-
#content(lang = '') ⇒ Object
content.
- #content=(v, lang = '') ⇒ Object
- #content_file_exists?(lang = '') ⇒ Boolean
- #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
-
#parent_title ⇒ Object
properties.
-
#path(lang = '') ⇒ Object
path.
- #set_basedirpath_from_parent ⇒ Object
- #set_basepath ⇒ Object
- #set_parent_from_basedirpath ⇒ Object
- #to_s ⇒ Object
Class Method Details
.base_dir ⇒ Object
settings and helpers
181 182 183 |
# File 'app/models/optimacms/template.rb', line 181 def self.base_dir Rails.root.to_s + '/app/views/' end |
.content_filename_ext(format) ⇒ Object
190 191 192 |
# File 'app/models/optimacms/template.rb', line 190 def self.content_filename_ext(format) EXTENSIONS[format] end |
.content_filename_lang_postfix(lang) ⇒ Object
185 186 187 188 |
# File 'app/models/optimacms/template.rb', line 185 def self.content_filename_lang_postfix(lang) return '' if lang=='' return '.'+lang end |
.create_folders_tree(dirpath) ⇒ Object
create folders if necessary - NOT WORKING
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'app/models/optimacms/template.rb', line 322 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 |
.get_where(values) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'app/models/optimacms/template.rb', line 88 def self.get_where(values) w = '1=1 ' #w << " AND uid like '#{values['uid']}%' " if values['uid'] && !values['uid'].blank? w << " AND title like '%#{values['title']}%' " if values['title'] && !values['title'].blank? #w << " AND parent_id = '#{values['parent_id']}' " if !values['parent_id']!=-1 w end |
.list(filter, pg) ⇒ Object
search
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 |
# File 'app/models/optimacms/template.rb', line 59 def self.list(filter, pg) pg =1 if pg.nil? || pg<=0 order = filter.order if order.empty? orderby = 'id' orderdir = 'asc' else orderby = order[0][0] orderdir = order[0][1] end #@items = Task.includes(:status, :client, :contacts).for_user(current_user.id).all #includes(:status, :client, :contacts).for_user(filter['user_id']) #includes(:status, :client, :contacts, :received_user) q = self.where(get_where(filter.data)) parent_id = filter.data['parent_id'].to_i if parent_id==0 q = q.roots elsif parent_id>0 q = q.children_of(parent_id.to_s) end q.order(" #{orderby} #{orderdir}").page(pg) end |
Instance Method Details
#_before_create ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'app/models/optimacms/template.rb', line 256 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
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'app/models/optimacms/template.rb', line 299 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
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'app/models/optimacms/template.rb', line 270 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_validation ⇒ Object
callbacks
242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'app/models/optimacms/template.rb', line 242 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
98 99 100 101 102 103 104 105 106 |
# File 'app/models/optimacms/template.rb', line 98 def attach # check if file exists if !content_file_exists? errors.add(:attach, "file not found") return false end save end |
#check_parent_folder_exists ⇒ Object
validations
139 140 141 142 143 |
# File 'app/models/optimacms/template.rb', line 139 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
165 166 167 168 169 170 |
# File 'app/models/optimacms/template.rb', line 165 def content(lang='') filename = fullpath(lang) return nil if filename.nil? return '' if !File.exists? filename File.read(filename) end |
#content=(v, lang = '') ⇒ Object
172 173 174 175 176 |
# File 'app/models/optimacms/template.rb', line 172 def content=(v, lang='') File.open(self.fullpath(lang), "w+") do |f| f.write(v) end end |
#content_file_exists?(lang = '') ⇒ Boolean
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/models/optimacms/template.rb', line 123 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_basedirpath ⇒ Object
208 209 210 211 212 213 |
# File 'app/models/optimacms/template.rb', line 208 def fix_basedirpath self.basedirpath ||= '' if !self.basedirpath.blank? && self.basedirpath[-1] !='/' self.basedirpath << '/' end end |
#folder_delete ⇒ Object
folders
351 352 353 354 |
# File 'app/models/optimacms/template.rb', line 351 def folder_delete dirpath = self.folder_fullpath FileUtils.remove_dir(dirpath) if Dir.exists? dirpath end |
#folder_empty? ⇒ Boolean
356 357 358 359 360 361 362 363 |
# File 'app/models/optimacms/template.rb', line 356 def folder_empty? dirpath = self.folder_fullpath if !Dir.exists? dirpath return true end return Dir.entries(dirpath).size == 2 end |
#folder_fullpath ⇒ Object
155 156 157 158 159 |
# File 'app/models/optimacms/template.rb', line 155 def folder_fullpath f = self.folder_path return nil if f.nil? Template.base_dir + f end |
#folder_path ⇒ Object
folder
150 151 152 153 |
# File 'app/models/optimacms/template.rb', line 150 def folder_path return nil if self.basepath.nil? self.basepath+'/' end |
#fullpath(lang = '') ⇒ Object
117 118 119 120 121 |
# File 'app/models/optimacms/template.rb', line 117 def fullpath(lang='') f = self.path(lang) return nil if f.nil? Template.base_dir + f end |
#has_code? ⇒ Boolean
48 49 50 |
# File 'app/models/optimacms/template.rb', line 48 def has_code? ['haml', 'erb'].include?(self.tpl_format) end |
#is_folder? ⇒ Boolean
52 53 54 |
# File 'app/models/optimacms/template.rb', line 52 def is_folder? self.is_folder end |
#parent_title ⇒ Object
properties
43 44 45 |
# File 'app/models/optimacms/template.rb', line 43 def parent_title self.parent.title end |
#path(lang = '') ⇒ Object
path
112 113 114 115 |
# File 'app/models/optimacms/template.rb', line 112 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_parent ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 |
# File 'app/models/optimacms/template.rb', line 215 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
196 197 198 199 200 201 202 203 204 205 |
# File 'app/models/optimacms/template.rb', line 196 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
227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'app/models/optimacms/template.rb', line 227 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
36 37 38 |
# File 'app/models/optimacms/template.rb', line 36 def to_s "#{title} (#{basepath})" end |