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.
21 22 23 |
# File 'app/models/html_asset.rb', line 21 def compiled @compiled end |
Class Method Details
.cache_key(system_id, name, type) ⇒ Object
98 99 100 |
# File 'app/models/html_asset.rb', line 98 def self.cache_key(system_id, name, type) "kit_htmlasset_#{name.downcase}.#{system_id}.#{type}" end |
.clear_cache(asset) ⇒ Object
102 103 104 |
# File 'app/models/html_asset.rb', line 102 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
144 145 146 147 148 149 150 151 152 |
# File 'app/models/html_asset.rb', line 144 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
106 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 |
# File 'app/models/html_asset.rb', line 106 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
40 41 42 43 44 45 46 |
# File 'app/models/html_asset.rb', line 40 def self.fetch(system_id, name, type) Rails.cache.fetch(HtmlAsset.cache_key(system_id, name.downcase, type), :expires_in=>1.second) 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
26 27 28 |
# File 'app/models/html_asset.rb', line 26 def display_name full_type end |
#full_type ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'app/models/html_asset.rb', line 154 def full_type if file_type=='js' "Javascript" elsif file_type=='css' "Stylesheet" else "Unknown" end end |
#generate_compiled ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'app/models/html_asset.rb', line 76 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
86 87 88 89 90 |
# File 'app/models/html_asset.rb', line 86 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
48 49 50 |
# File 'app/models/html_asset.rb', line 48 def generate_fingerprint self.fingerprint = Digest::MD5.hexdigest("#{self.updated_at}-#{self.name}.#{self.file_type}") end |
#generate_js ⇒ Object
92 93 94 95 96 |
# File 'app/models/html_asset.rb', line 92 def generate_js self.compiled = self.body return self end |
#history ⇒ Object
36 37 38 |
# File 'app/models/html_asset.rb', line 36 def history DesignHistory.sys(self.system_id).where(:model=>"HtmlAsset").where(:model_id=>self.id).order(:id) end |
#kit_name ⇒ Object
72 73 74 |
# File 'app/models/html_asset.rb', line 72 def kit_name "#{self.name.downcase}-#{self.fingerprint}.#{self.file_type}".downcase end |
#record_history ⇒ Object
30 31 32 33 34 |
# File 'app/models/html_asset.rb', line 30 def record_history if self.changed.include?('body') DesignHistory.record(self) end end |
#write_to_file ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/html_asset.rb', line 52 def write_to_file parent = File.join(Rails.root, "public", "kit", self.system_id.to_s, self.file_type) FileUtils.mkdir_p(parent) unless File.exists?(parent) path = File.join(Rails.root, "public", "kit", self.system_id.to_s, self.file_type, self.kit_name) dir = File.join(Rails.root, "public", "kit", self.system_id.to_s, 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 |