Class: Caboose::Theme
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Theme
- Defined in:
- app/models/caboose/theme.rb
Instance Method Summary collapse
- #asset_name(digest = self.digest, site_id = 0) ⇒ Object
- #asset_path(digest = self.digest, site_id = 0) ⇒ Object
- #asset_url(site_id = 0) ⇒ Object
- #compile(for_site_id = 0) ⇒ Object
- #delete_asset ⇒ Object
- #get_binding(site_id) ⇒ Object
- #js_path(site_id = 0, digest = self.js_digest) ⇒ Object
- #js_url(site_id = 0) ⇒ Object
- #revert ⇒ Object
- #update_cloudinary_banner ⇒ Object
- #update_digest(digest) ⇒ Object
- #upload_js(for_site_id = 0) ⇒ Object
Instance Method Details
#asset_name(digest = self.digest, site_id = 0) ⇒ Object
183 184 185 |
# File 'app/models/caboose/theme.rb', line 183 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
179 180 181 |
# File 'app/models/caboose/theme.rb', line 179 def asset_path(digest = self.digest, site_id = 0) "assets/themes/#{asset_name(digest, site_id)}.css" end |
#asset_url(site_id = 0) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/models/caboose/theme.rb', line 187 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
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'app/models/caboose/theme.rb', line 113 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}" old_digest = self.digest 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', :cache_control => 'public, max-age=86400') # if !old_digest.blank? # key = self.asset_path(old_digest, for_site_id) # obj = bucket.objects[key] # obj.delete # end 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) } if !old_digest.blank? path = File.join(Rails.root, 'public', self.asset_path(old_digest, for_site_id)) File.delete(path) if File.file?(path) end end self.update_digest(asset.digest) rescue Sass::SyntaxError => error if Rails.env.development? raise error end theme.sass_error = error theme.save theme.revert end end |
#delete_asset ⇒ Object
175 176 177 |
# File 'app/models/caboose/theme.rb', line 175 def delete_asset Caboose.log("deleting asset") end |
#get_binding(site_id) ⇒ Object
171 172 173 |
# File 'app/models/caboose/theme.rb', line 171 def get_binding(site_id) binding end |
#js_path(site_id = 0, digest = self.js_digest) ⇒ Object
200 201 202 |
# File 'app/models/caboose/theme.rb', line 200 def js_path(site_id = 0, digest = self.js_digest) "assets/themes/theme_#{self.id}_site_#{site_id}_#{digest}.js" end |
#js_url(site_id = 0) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'app/models/caboose/theme.rb', line 237 def js_url(site_id = 0) old = "https://cabooseit.s3.amazonaws.com/assets/natureseyenri/js/application.js" return old if self.main_js.blank? if Rails.env.production? return "https://#{ActionController::Base.asset_host}/#{js_path(site_id)}" else path = File.join(Rails.root, 'public', "#{js_path(site_id)}") if File.file?(path) return "#{ActionController::Base.asset_host}/#{js_path(site_id)}" else return "https://#{Caboose::cdn_domain}/#{js_path(site_id)}" end end end |
#revert ⇒ Object
167 168 169 |
# File 'app/models/caboose/theme.rb', line 167 def revert Caboose.log("reverting theme") end |
#update_cloudinary_banner ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 |
# File 'app/models/caboose/theme.rb', line 256 def if Caboose::use_cloudinary url = self..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. = result['version'] if result && result['version'] self.save end end end |
#update_digest(digest) ⇒ Object
252 253 254 |
# File 'app/models/caboose/theme.rb', line 252 def update_digest(digest) self.update_column('digest', digest) end |
#upload_js(for_site_id = 0) ⇒ Object
204 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 |
# File 'app/models/caboose/theme.rb', line 204 def upload_js(for_site_id = 0) str = "" js_digest = SecureRandom.urlsafe_base64.to_s old_digest = self.js_digest Caboose::ThemeFile.where("filename ILIKE (?)","%.js%").all.each do |jsfile| if Caboose::ThemeFileMembership.where(:theme_id => self.id, :theme_file_id => jsfile.id).exists? uri = URI(jsfile.filename) str += Net::HTTP.get(uri) end end str += self.main_js 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[self.js_path(for_site_id, js_digest)].write(str, :acl => 'public-read', :content_type => 'application/javascript', :cache_control => 'public, max-age=86400') # if !old_digest.blank? # key = self.js_path(for_site_id, old_digest) # obj = bucket.objects[key] # obj.delete # end 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', self.js_path(for_site_id, js_digest)), 'w') { |f| f.write( str ) } if !old_digest.blank? path = File.join(Rails.root, 'public', self.js_path(for_site_id, old_digest)) File.delete(path) if File.file?(path) end end self.update_column('js_digest', js_digest) end |