Class: Category

Inherits:
Data
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Linkable

#persisted?, #to_model

Instance Attribute Details

#slugObject (readonly)

Returns the value of attribute slug

Returns:

  • (Object)

    the current value of slug



5
6
7
# File 'lib/generators/frozen/templates/md/models/category.rb', line 5

def slug
  @slug
end

#titleObject (readonly)

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



5
6
7
# File 'lib/generators/frozen/templates/md/models/category.rb', line 5

def title
  @title
end

Class Method Details

.allObject



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

#pagesObject



32
33
34
# File 'lib/generators/frozen/templates/md/models/category.rb', line 32

def pages
  Page.where(category_slugs: slug)
end