Class: Caboose::Block

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/block.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_checkbox_multiple_value(b, arr) ⇒ Object

Parses the value given for a checkbox multiple block



368
369
370
371
372
373
374
375
376
377
378
# File 'app/models/caboose/block.rb', line 368

def self.parse_checkbox_multiple_value(b, arr)
  current_value = b.value ? b.value.split('|') : []
  v = arr[0]
  checked = arr[1].to_i == 1
  if checked && !current_value.include?(v)
    current_value << v      
  elsif !checked && current_value.include?(v)
    current_value.delete(v)
  end
  return current_value.join('|')
end

Instance Method Details

#block_message(block, ex) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'app/models/caboose/block.rb', line 211

def block_message(block, ex)    
  msg = block ? (block.block_type ? "Error with #{block.block_type.name} block (block_type_id #{block.block_type.id}, block_id #{block.id})\n" : "Error with block (block_id #{block.id})\n") : ''
  if ex.is_a?(String)
    Caboose.log("#{msg}#{ex}")
  else
    Caboose.log("#{msg}#{ex.message}\n#{ex.backtrace.join("\n")}")
  end
  return msg
end

#caste_valueObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/caboose/block.rb', line 39

def caste_value
  if self.block_type_id.nil?
    bt = Caboose::BlockType.where(:field_type => 'text').first
    if bt.nil?
      bt = Caboose::BlockType.create(:name => 'text', :description => 'Text', :field_type => 'text', :default => '', :width => 800, :height => 400, :fixed_placeholder => false)
    end      
    self.block_type_id = bt.id
  end
  #if self.block_type_id.field_type.nil?
  #  self.block_type.field_type = 'text'      
  #end        
  #if self.block_type.field_type == 'checkbox'
  #  v = self.value
  #  self.value = v ? (v == 1 || v == '1' || v == true ? 1 : 0) : 0        
  #end
end

#child(name) ⇒ Object



69
70
71
# File 'app/models/caboose/block.rb', line 69

def child(name)
  Caboose::Block.where("parent_id = ? and name = ?", self.id, name).first
end

#child_value(name) ⇒ Object



61
62
63
64
65
66
67
# File 'app/models/caboose/block.rb', line 61

def child_value(name)
  b = self.child(name)
  return nil if b.nil?
  return b.image if b.block_type.field_type == 'image'
  return b.file  if b.block_type.field_type == 'file'
  return b.value        
end

#constrain_allObject



420
421
422
423
424
425
# File 'app/models/caboose/block.rb', line 420

def constrain_all         
  self.children.each do |b|
    return false if b.full_width == true
  end
  return true
end

#create_children(block_type_override = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/caboose/block.rb', line 73

def create_children(block_type_override = nil)
  bt = block_type_override ? block_type_override : block_type
  bt.children.each do |bt2|
    bt_id = bt2.id      
    #if bt2.parent_id
    #  new_bt_id = Caboose::BlockType.where(:name => bt2.field_type).first.id 
    #end      
    if self.child(bt2.name).nil?
      b = Caboose::Block.create(
        :post_id => self.post_id,
        :page_id => self.page_id,
        :parent_id => self.id, 
        :block_type_id => bt_id,
        :name => bt2.name,
        :value => bt2.default
      )
      b.create_children(bt2)
    end
  end
end

#full_nameObject



56
57
58
59
# File 'app/models/caboose/block.rb', line 56

def full_name
  return self.name if parent_id.nil?
  return "#{parent.full_name}_#{self.name}"
end

#get_global_value(site_id) ⇒ Object

Returns the master block for this global block



335
336
337
338
339
340
341
342
343
344
345
# File 'app/models/caboose/block.rb', line 335

def get_global_value(site_id)
  return if !self.block_type.is_global    
  return if !Caboose::Block.includes(:page).where("blocks.id <> ? and block_type_id = ? and pages.site_id = ?", self.id, self.block_type_id, site_id).exists?        
  b = Caboose::Block.includes(:page).where("blocks.id <> ? and block_type_id = ? and pages.site_id = ?", self.id, self.block_type_id, site_id).reorder("blocks.id").first    
  self.value = b.value
  self.save
  
  self.children.each do |b2|
    b2.get_global_value(site_id) if b2.block_type.is_global
  end    
end

#js_hashObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/models/caboose/block.rb', line 284

def js_hash
  kids = self.children.collect { |b| b.js_hash }
  bt = self.block_type    
  return {
    'id'             => self.id,     
    'post_id'        => self.post_id,      
    'page_id'        => self.page_id,      
    'parent_id'      => self.parent_id,
    'parent_title'   => self.parent ? self.parent.title : '',
    'block_type_id'  => self.block_type_id,    
    'sort_order'     => self.sort_order,
    'name'           => self.name,
    'value'          => self.value,
    'children'       => kids,      
    'block_type'     => {      
      'id'                              => bt.id,                            
      'parent_id'                       => bt.parent_id,                     
      'name'                            => bt.name,
      'description'                     => bt.description,
      'render_function'                 => bt.render_function,
      'use_render_function'             => bt.use_render_function,
      'use_render_function_for_layout'  => bt.use_render_function_for_layout,
      'allow_child_blocks'              => bt.allow_child_blocks,
      'field_type'                      => bt.field_type,
      'default'                         => bt.default,
      'width'                           => bt.width,
      'height'                          => bt.height,
      'fixed_placeholder'               => bt.fixed_placeholder,
      'options'                         => bt.options,
      'options_function'                => bt.options_function,
      'options_url'                     => bt.options_url
    },
    'file' => {
      'url' => self.file.url
    },
    'image' => {
      'tiny_url'  => self.image.url(:tiny),
      'thumb_url' => self.image.url(:thumb),
      'large_url' => self.image.url(:large),
    }
  }
end

#log_helper(prefix = '') ⇒ Object



327
328
329
330
331
332
# File 'app/models/caboose/block.rb', line 327

def log_helper(prefix = '')
  puts "#{prefix}#{self.id}"
  self.children.each do |b|
    b.log_helper("#{prefix}-")
  end
end

#move_downObject

Move a block down



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'app/models/caboose/block.rb', line 401

def move_down
  siblings = Caboose::Block.where(:parent_id => self.parent_id).reorder(:sort_order).all        
  siblings.each_with_index do |b2, i|      
    b2.sort_order = i
    b2.save
  end
  changed = false
  siblings.each_with_index do |b2, i|      
    if i < (siblings.count-1) && b2.id == self.id                
      siblings[i+1].sort_order = i
      siblings[i+1].save        
      b2.sort_order = i + 1
      b2.save
      changed = true
    end      
  end
  return changed            
end

#move_upObject

Move a block up



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'app/models/caboose/block.rb', line 381

def move_up    
  siblings = Caboose::Block.where(:parent_id => self.parent_id).reorder(:sort_order).all        
  siblings.each_with_index do |b2, i|      
    b2.sort_order = i
    b2.save
  end
  changed = false
  siblings.each_with_index do |b2, i|      
    if i > 0 && b2.id == self.id                
      siblings[i-1].sort_order = i
      siblings[i-1].save        
      b2.sort_order = i - 1
      b2.save
      changed = true
    end      
  end
  return changed            
end

#partial(name, options) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'app/models/caboose/block.rb', line 229

def partial(name, options)    
  defaults = { :modal => false, :empty_text => '', :editing => false, :css => nil, :js => nil }
  options2 = nil
  if options.is_a?(Hash)
    options2 = defaults.merge(options)
  else
    options2 = { :modal => options.modal, :empty_text => options.empty_text, :editing => options.editing, :css => options.css, :js => options.js }        
  end
  options2[:block] = self
  
  view = ActionView::Base.new(ActionController::Base.view_paths)
  site = options[:site]
  
  begin
    str = view.render(:partial => "../../sites/#{site.name}/blocks/#{name}", :locals => options2)
  rescue ActionView::MissingTemplate => ex      
    begin
      str = view.render(:partial => "caboose/blocks/#{name}", :locals => options2)      
    rescue Exception => ex
      Caboose.log("Partial caboose/blocks/#{name} doesn't exist.")
      str = "<p class='note error'>#{self.partial_message(name, ex)}</p>"
    end      
  end
    
  return str
end

#partial_message(name, ex) ⇒ Object



256
257
258
259
260
261
262
263
264
# File 'app/models/caboose/block.rb', line 256

def partial_message(name, ex)    
  msg = "Error with partial #{name}:\n"
  if ex.is_a?(String)
    Caboose.log("#{msg}#{ex}")
  else
    Caboose.log("#{msg}#{ex.message}\n#{ex.backtrace.join("\n")}")
  end
  return msg
end

#render(block, options) ⇒ Object



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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
199
200
201
202
203
204
205
206
207
208
209
# File 'app/models/caboose/block.rb', line 94

def render(block, options)
  #Caboose.log("block.render\nself.id = #{self.id}\nblock = #{block}\nblock.full_name = #{block.full_name}\noptions.class = #{options.class}\noptions = #{options}")                
  if block && block.is_a?(String)
    #Caboose.log("Block #{block} is a string, finding block object... self.id = #{self.id}")                                          
    b = self.child(block)        
    if b.nil?
      self.create_children
      b = self.child(block)
      if b.nil?        
        Caboose.log("No block exists with name \"#{block}\".")
        return false
      end
    end
    block = b
  end        
  str = ""

  defaults = {
    :view => nil,
    :controller_view_content => nil,
    :modal => false,
    :empty_text => '',
    :editing => false,
    :css => nil,
    :js => nil,
    :csrf_meta_tags => nil,
    :csrf_meta_tags2 => nil,    
    :logged_in_user => nil
  }    
  #defaults = { :modal => false, :empty_text => '', :editing => false, :css => nil, :js => nil, :block => block }
  options2 = nil
  if options.is_a?(Hash)
    options2 = defaults.merge(options)
  else
    #options2 = { :modal => options.modal, :empty_text => options.empty_text, :editing => options.editing, :css => options.css, :js => options.js }
    options2 = {
      :view                    => options.view                    ? options.view                    : nil,
      :controller_view_content => options.controller_view_content ? options.controller_view_content : nil,
      :modal                   => options.modal                   ? options.modal                   : nil,
      :empty_text              => options.empty_text              ? options.empty_text              : nil,
      :editing                 => options.editing                 ? options.editing                 : nil,
      :css                     => options.css                     ? options.css                     : nil,
      :js                      => options.js                      ? options.js                      : nil,
      :csrf_meta_tags          => options.csrf_meta_tags          ? options.csrf_meta_tags          : nil,
      :csrf_meta_tags2         => options.csrf_meta_tags2         ? options.csrf_meta_tags2         : nil,
      :logged_in_user          => options.logged_in_user          ? options.logged_in_user          : nil
    }
  end
  options2[:block] = block

  view = options2[:view]     
  view = ActionView::Base.new(ActionController::Base.view_paths) if view.nil?
    
  if block.block_type.use_render_function && block.block_type.render_function
    begin
      str = view.render(:partial => "caboose/blocks/render_function", :locals => options2)
    rescue Exception => ex
      msg = block ? (block.block_type ? "Error with #{block.block_type.name} block (block_type_id #{block.block_type.id}, block_id #{block.id})\n" : "Error with block (block_id #{block.id})\n") : ''             
      Caboose.log("#{msg}#{ex.message}\n#{ex.backtrace.join("\n")}")
      str = "<p class='note error'>#{msg}</p>"
    end            
  else
    
    full_name = block.block_type.full_name
    full_name = "lksdjflskfjslkfjlskdfjlkjsdf" if full_name.nil? || full_name.length == 0
    
    # Check the local site first      
    site = options2[:site]
    if site.nil?
      self.block_message(block, "Error: site variable is nil.")
    end
      
    begin                        
      str = view.render(:partial => "../../sites/#{site.name}/blocks/#{full_name}", :locals => options2)        
    rescue ActionView::MissingTemplate => ex
      #Caboose.log("Can't find partial: ../../sites/#{site.name}/blocks/#{full_name}")
      begin
        str = view.render(:partial => "../../sites/#{site.name}/blocks/#{block.block_type.name}", :locals => options2)                    
      rescue ActionView::MissingTemplate => ex
        #Caboose.log("Can't find partial: ../../sites/#{site.name}/blocks/#{block.block_type.name}")
        begin                        
          str = view.render(:partial => "../../sites/#{site.name}/blocks/#{block.block_type.field_type}", :locals => options2)            
        rescue ActionView::MissingTemplate
          #Caboose.log("Can't find partial: ../../sites/#{site.name}/blocks/#{block.block_type.field_type}")
          begin
            str = view.render(:partial => "caboose/blocks/#{full_name}", :locals => options2)        
          rescue ActionView::MissingTemplate
            #Caboose.log("Can't find partial: caboose/blocks/#{full_name}")
            begin          
              str = view.render(:partial => "caboose/blocks/#{block.block_type.name}", :locals => options2)                    
            rescue ActionView::MissingTemplate
              #Caboose.log("Can't find partial: caboose/blocks/#{block.block_type.name}")
              begin                        
                str = view.render(:partial => "caboose/blocks/#{block.block_type.field_type}", :locals => options2)            
              rescue Exception => ex                  
                #Caboose.log("Can't find partial: caboose/blocks/#{block.block_type.field_type}")
                str = "<p class='note error'>#{self.block_message(block, ex)}</p>"
              end
            rescue Exception => ex                          
              str = "<p class='note error'>#{self.block_message(block, ex)}</p>"
            end
          rescue Exception => ex              
            str = "<p class='note error'>#{self.block_message(block, ex)}</p>"
          end
        rescue Exception => ex                  
          str = "<p class='note error'>#{self.block_message(block, ex)}</p>"
        end                              
      rescue Exception => ex                                                     
        str = "<p class='note error'>#{self.block_message(block, ex)}</p>"
      end        
    rescue Exception => ex        
      str = "<p class='note error'>#{self.block_message(block, ex)}</p>"
    end                    
  end    
  return str
end

#render_from_function(options) ⇒ Object



221
222
223
224
225
226
227
# File 'app/models/caboose/block.rb', line 221

def render_from_function(options)
  self.create_children    
  #locals = OpenStruct.new(:block => self, :empty_text => empty_text, :editing => editing)
  locals = OpenStruct.new(options)
  erb = ERB.new(block_type.render_function)
  return erb.result(locals.instance_eval { binding })
end

#titleObject

def child_block_link

return "<div class='new_block' id='new_block_#{self.id}'>New Block</div>"

end

def new_block_before_link

return "<div class='new_block_before' id='new_block_before_#{self.id}'>New Block</div>"

end

def new_block_after_link

return "<div class='new_block_after' id='new_block_after_#{self.id}'>New Block</div>"

end



276
277
278
279
280
281
282
# File 'app/models/caboose/block.rb', line 276

def title    
  str = "#{self.block_type.name}"
  if self.name && self.name.strip.length > 0
    str << " (#{self.name})"
  end
  return str
end

#update_global_value(value, site_id) ⇒ Object

Updates all the global blocks for the given site



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'app/models/caboose/block.rb', line 348

def update_global_value(value, site_id)
  return if !self.block_type.is_global    
  return if !Caboose::Block.includes(:page).where("blocks.id <> ? and block_type_id = ? and pages.site_id = ?", self.id, self.block_type_id, site_id).exists?        
  
  sql = nil
  if self.page_id
    sql = ["update blocks set value = ? where id in (
        select B.id from blocks B left join pages P on B.page_id = P.id
        where B.id <> ? and B.block_type_id = ? and P.site_id = ?
      )", value, self.id, self.block_type_id, site_id]
  elsif self.post_id      
    sql = ["update blocks set value = ? where id in (
      select B.id from blocks B left join posts P on B.post_id = P.id
      where B.id <> ? and B.block_type_id = ? and P.site_id = ?
    )", value, self.id, self.block_type_id, site_id]
  end
  ActiveRecord::Base.connection.execute(ActiveRecord::Base.send(:sanitize_sql_array, sql))            
end