Class: ActionPage::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
app/models/action_page/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Base

Returns a new instance of Base.



20
21
22
23
# File 'app/models/action_page/base.rb', line 20

def initialize(filename)
  @slug = File.basename(filename.split('.')[0])
  @frontmatter = YAML.load(File.read(filename)).with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

TODO: Define attribute methods to speed this up



26
27
28
29
30
31
# File 'app/models/action_page/base.rb', line 26

def method_missing(m, *args, &block)
  if value = @frontmatter[m]
    return value
  end
  super
end

Instance Attribute Details

#slugObject (readonly)

Returns the value of attribute slug.



4
5
6
# File 'app/models/action_page/base.rb', line 4

def slug
  @slug
end

Class Method Details

.allObject



6
7
8
9
10
# File 'app/models/action_page/base.rb', line 6

def self.all
  Dir[pages_directory]
    .reject { %w(index show).include? File.basename(_1.split('.')[0]) }
    .map { |f| new(f) }
end

.find(slug) ⇒ Object



12
13
14
# File 'app/models/action_page/base.rb', line 12

def self.find(slug)
  all.find { _1.slug == slug }
end

.pages_directoryObject



16
17
18
# File 'app/models/action_page/base.rb', line 16

def self.pages_directory
  Rails.root.join("app", "views", view_key, '*.yaml').freeze
end

.view_keyObject



41
42
43
# File 'app/models/action_page/base.rb', line 41

def self.view_key
  model_name.route_key
end

Instance Method Details

#respond_to_missing(m) ⇒ Object



33
34
35
# File 'app/models/action_page/base.rb', line 33

def respond_to_missing(m)
  @frontmatter.key(m)
end

#to_paramObject



37
38
39
# File 'app/models/action_page/base.rb', line 37

def to_param
  slug
end