Class: Caboose::Site

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.id_for_domain(domain) ⇒ Object



47
48
49
50
51
# File 'app/models/caboose/site.rb', line 47

def self.id_for_domain(domain)
  d = Caboose::Domain.where(:domain => domain).first
  return nil if d.nil?
  return d.site_id
end

.sanitize_name(name) ⇒ Object



53
54
55
# File 'app/models/caboose/site.rb', line 53

def self.sanitize_name(name)
  self.name = self.name.downcase.gsub(' ', '_')
end

Instance Method Details

#custom_cssObject



82
83
84
85
86
87
88
89
# File 'app/models/caboose/site.rb', line 82

def custom_css    
  resp = HTTParty.get(self.custom_css_url)
  if resp.nil? || resp.code.to_i == 403
    self.custom_css = ""
    return ""            
  end
  return resp.body    
end

#custom_css=(str) ⇒ Object



100
101
102
103
104
105
106
107
# File 'app/models/caboose/site.rb', line 100

def custom_css=(str)    
  config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env]
  AWS.config(:access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key'])
  bucket =  AWS::S3.new.buckets[config['bucket']]                         
  bucket.objects["assets/#{self.name}/css/custom.css"].write(str, :acl => 'public-read')                        
  self.date_css_updated = DateTime.now.utc
  self.save
end

#custom_css_urlObject



67
68
69
70
71
# File 'app/models/caboose/site.rb', line 67

def custom_css_url
  url = "http://#{Caboose::cdn_domain}/assets/#{self.name}/css/custom.css"
  url << "?#{self.date_css_updated.strftime('%Y%m%d%H%M%S')}" if self.date_css_updated
  return url
end

#custom_jsObject



73
74
75
76
77
78
79
80
# File 'app/models/caboose/site.rb', line 73

def custom_js              
  resp = HTTParty.get(self.custom_js_url)
  if resp.nil? || resp.code.to_i == 403
    self.custom_js = ""
    return ""            
  end
  return resp.body    
end

#custom_js=(str) ⇒ Object



91
92
93
94
95
96
97
98
# File 'app/models/caboose/site.rb', line 91

def custom_js=(str)    
  config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env]
  AWS.config(:access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key'])
  bucket =  AWS::S3.new.buckets[config['bucket']]                         
  bucket.objects["assets/#{self.name}/js/custom.js"].write(str, :acl => 'public-read')                        
  self.date_js_updated = DateTime.now.utc
  self.save
end

#custom_js_urlObject



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

def custom_js_url
  url = "http://#{Caboose::cdn_domain}/assets/#{self.name}/js/custom.js"
  url << "?#{self.date_js_updated.strftime('%Y%m%d%H%M%S')}" if self.date_js_updated
  return url
end

#default_layoutObject



24
25
26
27
# File 'app/models/caboose/site.rb', line 24

def default_layout    
  return Caboose::BlockType.where(:id => self.default_layout_id).first if self.default_layout_id
  return Caboose::BlockType.where(:name => 'layout_basic').first
end

#init_users_and_rolesObject



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

def init_users_and_roles
      
  admin_user = Caboose::User.where( :site_id => self.id, :username => 'admin').first    
  admin_user = Caboose::User.create(:site_id => self.id, :username => 'admin', :email => '[email protected]', :password => Digest::SHA1.hexdigest(Caboose::salt + 'caboose')) if admin_user.nil?                          
  admin_role = Caboose::Role.where( :site_id => self.id, :name => 'Admin').first    
  admin_role = Caboose::Role.create(:site_id => self.id, :parent_id => -1, :name => 'Admin') if admin_role.nil?    
  elo_user   = Caboose::User.where( :site_id => self.id, :username => 'elo').first    
  elo_user   = Caboose::User.create(:site_id => self.id, :username => 'elo', :email => '[email protected]') if elo_user.nil?    
  elo_role   = Caboose::Role.where( :site_id => self.id, :name => 'Everyone Logged Out').first
  elo_role   = Caboose::Role.create(:site_id => self.id, :name => 'Everyone Logged Out', :parent_id => -1) if elo_role.nil?    
  eli_user   = Caboose::User.where( :site_id => self.id, :username => 'eli').first    
  eli_user   = Caboose::User.create(:site_id => self.id, :username => 'eli', :email => '[email protected]') if eli_user.nil?    
  eli_role   = Caboose::Role.where( :site_id => self.id, :name => 'Everyone Logged In').first
  eli_role   = Caboose::Role.create(:site_id => self.id, :name => 'Everyone Logged In', :parent_id => elo_role.id) if eli_role.nil?
  
  # Make sure the admin role has the admin "all" permission
  admin_perm = Caboose::Permission.where(:resource => 'all', :action => 'all').first
  rp = Caboose::RolePermission.where(:role_id => admin_role.id, :permission_id => admin_perm.id).first
  rp = Caboose::RolePermission.create(:role_id => admin_role.id, :permission_id => admin_perm.id) if rp.nil?
  
  # Make sure the admin user is a member of the admin role
  rm = Caboose::RoleMembership.where(:role_id => admin_role.id, :user_id => admin_user.id).first
  rm = Caboose::RoleMembership.create(:role_id => admin_role.id, :user_id => admin_user.id) if rm.nil?
  
  # Make sure the elo user is a member of the elo role
  rm = Caboose::RoleMembership.where( :role_id => elo_role.id, :user_id => elo_user.id).first
  rm = Caboose::RoleMembership.create(:role_id => elo_role.id, :user_id => elo_user.id) if rm.nil?
  
  # Make sure the eli user is a member of the eli role
  rm = Caboose::RoleMembership.where( :role_id => eli_role.id, :user_id => eli_user.id).first
  rm = Caboose::RoleMembership.create(:role_id => eli_role.id, :user_id => eli_user.id) if rm.nil?
      
end

#primary_domainObject



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

def primary_domain
  Caboose::Domain.where(:site_id => self.id, :primary => true).first
end

#retargeting_configObject



41
42
43
44
45
# File 'app/models/caboose/site.rb', line 41

def retargeting_config
  c = Caboose::RetargetingConfig.where(:site_id => self.id).first
  c = Caboose::RetargetingConfig.create(:site_id => self.id) if c.nil?      
  return c      
end

#smtp_configObject



29
30
31
32
33
# File 'app/models/caboose/site.rb', line 29

def smtp_config
  c = Caboose::SmtpConfig.where(:site_id => self.id).first
  c = Caboose::SmtpConfig.create(:site_id => self.id) if c.nil?
  return c
end

#social_configObject



35
36
37
38
39
# File 'app/models/caboose/site.rb', line 35

def social_config
  s = Caboose::SocialConfig.where(:site_id => self.id).first
  s = Caboose::SocialConfig.create(:site_id => self.id) if s.nil?
  return s
end