Class: HtmlAsset
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- HtmlAsset
- Defined in:
- app/models/html_asset.rb
Instance Attribute Summary collapse
-
#compiled ⇒ Object
Returns the value of attribute compiled.
Class Method Summary collapse
- .cache_key(system_id, name, type) ⇒ Object
- .clear_cache(asset) ⇒ Object
- .create_default(sid, user_id, type) ⇒ Object
- .default_body(type) ⇒ Object
- .fetch(system_id, name, type) ⇒ Object
Instance Method Summary collapse
- #display_name ⇒ Object
- #full_type ⇒ Object
- #generate_compiled ⇒ Object
- #generate_css ⇒ Object
- #generate_fingerprint ⇒ Object
- #generate_js ⇒ Object
- #history ⇒ Object
- #kit_name ⇒ Object
- #record_history ⇒ Object
- #write_to_file ⇒ Object
Instance Attribute Details
#compiled ⇒ Object
Returns the value of attribute compiled.
7 8 9 |
# File 'app/models/html_asset.rb', line 7 def compiled @compiled end |
Class Method Details
.cache_key(system_id, name, type) ⇒ Object
83 84 85 |
# File 'app/models/html_asset.rb', line 83 def self.cache_key(system_id, name, type) "kit_htmlasset_#{name.downcase}.#{system_id}.#{type}" end |
.clear_cache(asset) ⇒ Object
87 88 89 |
# File 'app/models/html_asset.rb', line 87 def self.clear_cache(asset) Rails.cache.delete(HtmlAsset.cache_key(asset.system_id, asset.name, asset.file_type)) end |
.create_default(sid, user_id, type) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'app/models/html_asset.rb', line 129 def self.create_default(sid, user_id, type) asset = HtmlAsset.new asset.system_id = sid asset.file_type = type asset.name = 'application' asset.body = HtmlAsset.default_body(type) asset.user_id = user_id asset.save end |
.default_body(type) ⇒ Object
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 126 127 |
# File 'app/models/html_asset.rb', line 91 def self.default_body(type) if type == 'css' %Q[ /* Sassy CSS Stylesheet Examples - can use normal CSS3, but also: */ $var: #123; $big: 12px; .example { color: $var; span: { font-size: $big; } } .more-example { @extend .example; background-color: #000; } @mixin left_with_margin($m) { float: left; margin: $m; } #data { @include left_with_margin(10px); } ] elsif type == 'js' %Q[ // Javascript ] else "" end end |
.fetch(system_id, name, type) ⇒ Object
25 26 27 28 29 30 31 |
# File 'app/models/html_asset.rb', line 25 def self.fetch(system_id, name, type) Rails.cache.fetch(HtmlAsset.cache_key(system_id, name.downcase, type), :expires_in=>5.minutes) do asset = HtmlAsset.sys(system_id).where(:name=>name.downcase).where(:file_type=>type).first asset.write_to_file if asset asset end end |
Instance Method Details
#display_name ⇒ Object
11 12 13 |
# File 'app/models/html_asset.rb', line 11 def display_name full_type end |
#full_type ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'app/models/html_asset.rb', line 139 def full_type if file_type=='js' "Javascript" elsif file_type=='css' "Stylesheet" else "Unknown" end end |
#generate_compiled ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'app/models/html_asset.rb', line 61 def generate_compiled if self.file_type=='css' self.generate_css elsif self.file_type=='js' self.generate_js else self.compiled = self.body end end |
#generate_css ⇒ Object
71 72 73 74 75 |
# File 'app/models/html_asset.rb', line 71 def generate_css self.compiled = Sass::Engine.new(self.body, :syntax=>:scss, :style=>Rails.env=='development' ? :expanded : :compressed).to_css return self end |
#generate_fingerprint ⇒ Object
33 34 35 |
# File 'app/models/html_asset.rb', line 33 def generate_fingerprint self.fingerprint = Digest::MD5.hexdigest("#{self.updated_at}-#{self.name}.#{self.file_type}") end |
#generate_js ⇒ Object
77 78 79 80 81 |
# File 'app/models/html_asset.rb', line 77 def generate_js self.compiled = self.body return self end |
#history ⇒ Object
21 22 23 |
# File 'app/models/html_asset.rb', line 21 def history DesignHistory.sys(self.system_id).where(:model=>"HtmlAsset").where(:model_id=>self.id).order(:id) end |
#kit_name ⇒ Object
57 58 59 |
# File 'app/models/html_asset.rb', line 57 def kit_name "#{self.name.downcase}-#{self.fingerprint}.#{self.file_type}".downcase end |
#record_history ⇒ Object
15 16 17 18 19 |
# File 'app/models/html_asset.rb', line 15 def record_history if self.changed.include?('body') DesignHistory.record(self) end end |
#write_to_file ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/html_asset.rb', line 37 def write_to_file parent = File.join(Rails.root, "public", "kit", self.file_type) FileUtils.mkdir_p(parent) unless File.exists?(parent) path = File.join(Rails.root, "public", "kit", self.file_type, self.kit_name) dir = File.join(Rails.root, "public", "kit", self.file_type) found = false Dir.glob(dir + "/#{self.name.downcase}*") do |f| if f==path found = true else File.delete(f) end end unless found self.generate_compiled unless self.compiled File.open(path, "w") { |f| f.write(self.compiled) } end end |