Class: Village::FileModel
- Inherits:
-
Object
- Object
- Village::FileModel
- Extended by:
- ActiveModel::Naming
- Includes:
- Attributes
- Defined in:
- lib/village/file_model.rb
Instance Attribute Summary collapse
-
#content_path ⇒ Object
Returns the value of attribute content_path.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#folders ⇒ Object
Returns the value of attribute folders.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#to_param ⇒ Object
Returns the value of attribute to_param.
Attributes included from Attributes
Class Method Summary collapse
Instance Method Summary collapse
-
#content_html ⇒ Object
end self.create_class_methods_on.
-
#initialize(path) ⇒ FileModel
constructor
A new instance of FileModel.
- #to_key ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(path) ⇒ FileModel
Returns a new instance of FileModel.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/village/file_model.rb', line 10 def initialize(path) @content_path, @to_param, @folders, @slug, @extension = path.match(/^#{Rails.root}\/(#{self.class::CONTENT_PATH})\/((.*\/)?(.*))(\.[^.]+)$/).captures content = File.read(path) @attributes = Village::Config.clone(:title, :subtitle, :author, :layout) if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m @attributes.merge! YAML.load($1) @attributes[:content] = content[($1.size + $2.size)..-1] else @attributes[:content] = content end @attributes = (@attributes || {}).with_indifferent_access end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Village::Attributes
Instance Attribute Details
#content_path ⇒ Object
Returns the value of attribute content_path.
8 9 10 |
# File 'lib/village/file_model.rb', line 8 def content_path @content_path end |
#extension ⇒ Object
Returns the value of attribute extension.
8 9 10 |
# File 'lib/village/file_model.rb', line 8 def extension @extension end |
#folders ⇒ Object
Returns the value of attribute folders.
8 9 10 |
# File 'lib/village/file_model.rb', line 8 def folders @folders end |
#slug ⇒ Object
Returns the value of attribute slug.
8 9 10 |
# File 'lib/village/file_model.rb', line 8 def slug @slug end |
#to_param ⇒ Object
Returns the value of attribute to_param.
8 9 10 |
# File 'lib/village/file_model.rb', line 8 def to_param @to_param end |
Class Method Details
.create_class_methods_on(klass) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/village/file_model.rb', line 23 def self.create_class_methods_on(klass) klass.instance_eval do def all @files ||= Dir.glob("#{Rails.root}/#{self::CONTENT_PATH}/**/*.{#{Village::Config.file_extensions.join(',')}}").map do |filename| new filename end end def where(conditions = {}) conditions = conditions.symbolize_keys conditions.assert_valid_keys :to_param all.select do |article| conditions.all? do |key, value| article.send(key) == value end end end def find(id) where(:to_param => id).first or raise_missing_template(id.inspect) end def first all.first end def last all.last end def reset! @files = nil end private def raise_missing_template(id) if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR >= 1 raise ActionView::MissingTemplate.new( [self::CONTENT_PATH], id, "Invalid ID", false, ActionView::Template.template_handler_extensions) else raise ActionView::MissingTemplate.new( [self::CONTENT_PATH], id, "Invalid ID", false) end end end # end child.instance_eval end |
Instance Method Details
#content_html ⇒ Object
end self.create_class_methods_on
70 71 72 |
# File 'lib/village/file_model.rb', line 70 def content_html Tilt[@extension].new { @attributes[:content] }.render.html_safe if @attributes[:content].present? end |
#to_key ⇒ Object
78 79 80 |
# File 'lib/village/file_model.rb', line 78 def to_key [@to_param] end |
#to_s ⇒ Object
74 75 76 |
# File 'lib/village/file_model.rb', line 74 def to_s "#{@attributes[:title]} (#{@to_param})" end |