Class: Caboose::Theme

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

Instance Method Summary collapse

Instance Method Details

#asset_name(digest = self.digest, site_id = 0) ⇒ Object


165
166
167
# File 'app/models/caboose/theme.rb', line 165

def asset_name(digest = self.digest, site_id = 0)
	"theme_#{self.id}_site_#{site_id}-#{digest}"
end

#asset_path(digest = self.digest, site_id = 0) ⇒ Object


161
162
163
# File 'app/models/caboose/theme.rb', line 161

def asset_path(digest = self.digest, site_id = 0)
	"assets/themes/#{asset_name(digest, site_id)}.css"
end

#asset_url(site_id = 0) ⇒ Object


169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/caboose/theme.rb', line 169

def asset_url(site_id = 0)
	if Rails.env.production?
		return "https://#{ActionController::Base.asset_host}/#{asset_path(self.digest,site_id)}"
	else
		path = File.join(Rails.root, 'public', "#{asset_path(self.digest,site_id)}")
		if File.file?(path)
			return "#{ActionController::Base.asset_host}/#{asset_path(self.digest,site_id)}"
		else
			return "https://#{Caboose::cdn_domain}/#{asset_path(self.digest,site_id)}"
		end
	end
end

#compile(for_site_id = 0) ⇒ Object


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

def compile(for_site_id = 0)
	theme = self
	theme_name = 'default'
	path = Rails.root.join('themes', "#{theme_name}.scss.erb")
	body = ERB.new(File.read(File.join(path))).result(theme.get_binding(for_site_id))
	tmp_themes_path = File.join(Rails.root, 'tmp', 'themes')
	tmp_asset_name = "theme_#{self.id}_site_#{for_site_id}"
	FileUtils.mkdir_p(tmp_themes_path) unless File.directory?(tmp_themes_path)
	File.open(File.join(tmp_themes_path, "#{tmp_asset_name}.scss"), 'w') { |f| f.write(body) }
begin
   env = if Rails.application.assets.is_a?(Sprockets::Index)
     Rails.application.assets.instance_variable_get('@environment')
   else
     Rails.application.assets
   end
   asset = env.find_asset(tmp_asset_name)
   compressed_body = ::Sass::Engine.new(asset.body, {
     :syntax => :scss,
     :cache => false,
     :read_cache => false,
     :style => :compressed
   }).render
   str = StringIO.new(compressed_body)
   if Rails.env.production?
     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[theme.asset_path(asset.digest, for_site_id)].write(str, :acl => 'public-read', :content_type => 'text/css')
   else
   	theme_path = File.join(Rails.root, 'public', 'assets', 'themes')
		FileUtils.mkdir_p(theme_path) unless File.directory?(theme_path)
     File.open(File.join(Rails.root, 'public', theme.asset_path(asset.digest, for_site_id)), 'w') { |f| f.write(compressed_body) }
   end
   self.update_digest(asset.digest)
rescue Sass::SyntaxError => error
	if Rails.env.development?
		raise error
	end
	theme.revert
end
end

#delete_assetObject


157
158
159
# File 'app/models/caboose/theme.rb', line 157

def delete_asset
	Caboose.log("deleting asset")
end

#get_binding(site_id) ⇒ Object


153
154
155
# File 'app/models/caboose/theme.rb', line 153

def get_binding(site_id)
	binding
end

#js_urlObject


182
183
184
185
# File 'app/models/caboose/theme.rb', line 182

def js_url
	# TODO - figure out how to do this 
	"https://cabooseit.s3.amazonaws.com/assets/natureseyenri/js/application.js"
end

#revertObject


149
150
151
# File 'app/models/caboose/theme.rb', line 149

def revert
	Caboose.log("reverting theme")
end

#update_cloudinary_bannerObject


191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/caboose/theme.rb', line 191

def update_cloudinary_banner
	if Caboose::use_cloudinary
		url = self.default_banner_image.url(:huge)
		url = "https:#{url}" if !url.include?('https')
		if !url.include?('res.cloudinary')
      result = Cloudinary::Uploader.upload(url, :public_id => "caboose/banner_images/#{self.id}_huge", :overwrite => true)
      self.cl_banner_version = result['version'] if result && result['version']
      self.save
    end
   end
end

#update_digest(digest) ⇒ Object


187
188
189
# File 'app/models/caboose/theme.rb', line 187

def update_digest(digest)
	self.update_column('digest', digest)
end