Class: Category
- Inherits:
-
Data
- Object
- Data
- Category
- Includes:
- Linkable
- Defined in:
- lib/generators/frozen/templates/md/models/category.rb
Overview
This is a demo how to build categories without tying yourself to the DB. This keeps the “Markdown area” of your app separate from the “DB area”. A nicer approach for faster editing via the UI would probably be to introduce “Category” and “CategoryPage” as models.
Instance Attribute Summary collapse
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
Methods included from Linkable
Instance Attribute Details
#slug ⇒ Object (readonly)
Returns the value of attribute slug
5 6 7 |
# File 'lib/generators/frozen/templates/md/models/category.rb', line 5 def slug @slug end |
#title ⇒ Object (readonly)
Returns the value of attribute title
5 6 7 |
# File 'lib/generators/frozen/templates/md/models/category.rb', line 5 def title @title end |
Class Method Details
.all ⇒ Object
11 12 13 14 15 16 |
# File 'lib/generators/frozen/templates/md/models/category.rb', line 11 def self.all [ new(slug: "intro", title: "How it began"), new(slug: "outro", title: "How it ended") ] end |
.find(slug) ⇒ Object
28 29 30 |
# File 'lib/generators/frozen/templates/md/models/category.rb', line 28 def self.find(slug) Category.where(slug: slug).first or raise ActiveRecord::RecordNotFound end |
.where(**kwargs) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/generators/frozen/templates/md/models/category.rb', line 18 def self.where(**kwargs) scope = all kwargs.each do |key, value| if value.to_s != "all" scope.select! { |category| Array.wrap(category.send(key)).intersect?(Array.wrap(value)) } end end scope end |
Instance Method Details
#pages ⇒ Object
32 33 34 |
# File 'lib/generators/frozen/templates/md/models/category.rb', line 32 def pages Page.where(category_slugs: slug) end |