Class: Caboose::BlockType

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

Instance Method Summary collapse

Instance Method Details

#add_to_site(site_id) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/caboose/block_type.rb', line 125

def add_to_site(site_id)          
  if site_id == 'all'
    Caboose::BlockTypeSiteMembership.where(:block_type_id => self.id).destroy_all      
    Caboose::Site.reorder(:name).all.each do |site|
      Caboose::BlockTypeSiteMembership.create(:block_type_id => self.id, :site_id => site.id)
    end                          
  else
    if !Caboose::BlockTypeSiteMembership.where(:block_type_id => self.id, :site_id => site_id.to_i).exists?
      btsm = Caboose::BlockTypeSiteMembership.new 
      btsm.block_type_id = self.id
      btsm.site_id = site_id.to_i
      btsm.save
      return btsm.id
    end      
  end
end

#api_hashObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/caboose/block_type.rb', line 52

def api_hash
  return {
    :name                            => self.name,
    :description                     => self.description,
    :block_type_category_id          => self.block_type_category_id,
    :render_function                 => self.render_function,
    :use_render_function             => self.use_render_function,
    :use_render_function_for_layout  => self.use_render_function_for_layout,
    :allow_child_blocks              => self.allow_child_blocks,
    :field_type                      => self.field_type,
    :default                         => self.default,
    :width                           => self.width,
    :height                          => self.height,
    :fixed_placeholder               => self.fixed_placeholder,
    :options                         => self.options,
    :options_function                => self.options_function,
    :options_url                     => self.options_url,
    :children                        => self.api_hash_children
  }
end

#api_hash_childrenObject



73
74
75
76
# File 'app/models/caboose/block_type.rb', line 73

def api_hash_children
  return nil if self.children.nil? || self.children.count == 0    
  return self.children.collect { |bt| bt.api_hash }
end

#child(name) ⇒ Object



48
49
50
# File 'app/models/caboose/block_type.rb', line 48

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

#full_nameObject



39
40
41
42
# File 'app/models/caboose/block_type.rb', line 39

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

#parse_api_hash(h) ⇒ Object



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
# File 'app/models/caboose/block_type.rb', line 78

def parse_api_hash(h)    
  #Caboose.log(h)
  self.name                            = h['name']
  self.description                     = h['description']
  self.block_type_category_id          = h['block_type_category_id']
  self.render_function                 = h['render_function']
  self.use_render_function             = h['use_render_function']
  self.use_render_function_for_layout  = h['use_render_function_for_layout']
  self.allow_child_blocks              = h['allow_child_blocks']
  self.field_type                      = h['field_type']
  self.default                         = h['default']
  self.width                           = h['width']
  self.height                          = h['height']
  self.fixed_placeholder               = h['fixed_placeholder']
  self.options                         = h['options']
  self.options_function                = h['options_function']
  self.options_url                     = h['options_url']
  self.icon                            = h['icon']
  self.save
  
  # Remove any named children that don't exist in the given hash
  if h['children'].nil?
    Caboose::BlockType.where("parent_id = ? and name is not null", self.id).destroy_all
  else
    new_child_names = h['children'].collect { |h2| h2['name'] }      
    Caboose::BlockType.where("parent_id = ? and name is not null and name not in (?)", self.id, new_child_names).destroy_all
  end
  
  # Now add/update all the children
  if h['children']
    h['children'].each do |h2|
      bt = self.child(h2['name'])
      bt = Caboose::BlockType.create(:parent_id => self.id) if bt.nil?
      bt.parse_api_hash(h2)
    end
  end
  
end

#remove_from_site(site_id) ⇒ Object



142
143
144
145
146
147
148
# File 'app/models/caboose/block_type.rb', line 142

def remove_from_site(site_id)
  if site_id == 'all'
    Caboose::BlockTypeSiteMembership.where(:block_type_id => self.id).destroy_all                          
  else
    Caboose::BlockTypeSiteMembership.where(:block_type_id => self.id, :site_id => site_id.to_i).destroy_all      
  end
end

#render_options(site_id) ⇒ Object



44
45
46
# File 'app/models/caboose/block_type.rb', line 44

def render_options(site_id)    
  return eval(self.options_function)    
end

#toggle_site(site_id, value) ⇒ Object



117
118
119
120
121
122
123
# File 'app/models/caboose/block_type.rb', line 117

def toggle_site(site_id, value)          
  if value.to_i > 0
    return self.add_to_site(site_id)
  else
    return self.remove_from_site(site_id)
  end
end