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


141
142
143
144
145
# File 'app/models/caboose/site.rb', line 141

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


151
152
153
# File 'app/models/caboose/site.rb', line 151

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

Instance Method Details

#build_new_siteObject


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

def build_new_site
 # if defined?(SuiteBuilder) == 'constant' && SuiteBuilder.class == Class  
    helper = Caboose::SiteBuilder.new(self.name)
    helper.create_site_blocks(self.id)
 # end
#  self.init_users_and_roles
end

#custom_cssObject


179
180
181
182
183
184
185
# File 'app/models/caboose/site.rb', line 179

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

#custom_css=(str) ⇒ Object


196
197
198
199
200
201
202
203
# File 'app/models/caboose/site.rb', line 196

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', :content_type => 'text/css')                        
  self.date_css_updated = DateTime.now.utc
  self.save
end

#custom_css_urlObject


165
166
167
168
169
# File 'app/models/caboose/site.rb', line 165

def custom_css_url
  url = "//#{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


171
172
173
174
175
176
177
# File 'app/models/caboose/site.rb', line 171

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

#custom_js=(str) ⇒ Object


187
188
189
190
191
192
193
194
# File 'app/models/caboose/site.rb', line 187

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', :content_type => 'application/javascript')                        
  self.date_js_updated = DateTime.now.utc
  self.save
end

#custom_js_urlObject


159
160
161
162
163
# File 'app/models/caboose/site.rb', line 159

def custom_js_url
  url = "//#{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


106
107
108
109
# File 'app/models/caboose/site.rb', line 106

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


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/models/caboose/site.rb', line 205

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


155
156
157
# File 'app/models/caboose/site.rb', line 155

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

#product_defaultObject


129
130
131
132
133
# File 'app/models/caboose/site.rb', line 129

def product_default
  pd = Caboose::ProductDefault.where(:site_id => self.id).first
  pd = Caboose::ProductDefault.create(:site_id => self.id) if pd.nil?
  return pd    
end

#retargeting_configObject


123
124
125
126
127
# File 'app/models/caboose/site.rb', line 123

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


111
112
113
114
115
# File 'app/models/caboose/site.rb', line 111

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


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

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

#themeObject


72
73
74
# File 'app/models/caboose/site.rb', line 72

def theme
  Caboose::Theme.where(:id => self.theme_id).first
end

#update_cloudinary_faviconObject


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

def update_cloudinary_favicon
  if Caboose::use_cloudinary
    result = Cloudinary::Uploader.upload("https:#{self.favicon.url(:thumb)}" , :public_id => "caboose/favicons/#{self.id}_thumb", :overwrite => true)
    self.cl_favicon_version = result['version'] if result && result['version']
    self.save
  end
end

#update_cloudinary_logoObject


76
77
78
79
80
81
82
# File 'app/models/caboose/site.rb', line 76

def 
  if Caboose::use_cloudinary
    result = Cloudinary::Uploader.upload("https:#{self..url(:large)}" , :public_id => "caboose/site_logos/#{self.id}_large", :overwrite => true)
    self.cl_logo_version = result['version'] if result && result['version']
    self.save
  end
end

#valid_domainsObject


147
148
149
# File 'app/models/caboose/site.rb', line 147

def valid_domains
  self.domains.map{ |d| d.domain.blank? ? "" : d.domain.gsub(":3000","") }
end

#validate_presence_of_store_configObject


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

def validate_presence_of_store_config
  if self.use_store && !Caboose::StoreConfig.where(:site_id => self.id).exists?
    Caboose::StoreConfig.create(:site_id => self.id)
  end
end

#variant_defaultObject


135
136
137
138
139
# File 'app/models/caboose/site.rb', line 135

def variant_default
  vd = Caboose::VariantDefault.where(:site_id => self.id).first
  vd = Caboose::VariantDefault.create(:site_id => self.id) if vd.nil?
  return vd
end