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



143
144
145
# File 'app/models/caboose/theme.rb', line 143

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



139
140
141
# File 'app/models/caboose/theme.rb', line 139

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

#asset_url(site_id = 0) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/models/caboose/theme.rb', line 147

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



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
116
117
118
119
120
121
122
123
124
125
# File 'app/models/caboose/theme.rb', line 85

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



135
136
137
# File 'app/models/caboose/theme.rb', line 135

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

#get_binding(site_id) ⇒ Object



131
132
133
# File 'app/models/caboose/theme.rb', line 131

def get_binding(site_id)
	binding
end

#js_urlObject



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

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

#revertObject



127
128
129
# File 'app/models/caboose/theme.rb', line 127

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

#update_digest(digest) ⇒ Object



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

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