Class: Page

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/page.rb

Overview

defining model class to interact with database. It inherits the rails’s base class ActiveRecord::Base

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_all_pagesObject

Returns : Array.

Returns:

  • : Array



69
70
71
72
73
74
# File 'app/models/page.rb', line 69

def self.find_all_pages

  #finding title and id of all pages
  return self.select("id, title")

end

.find_by_path(path) ⇒ Object

Returns : Array(page).

Returns:

  • : Array(page)



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/page.rb', line 104

def self.find_by_path(path)

  # split the path param and assign it to a variable, and reject spliting if it is blank
  split_path = path.to_s.split('/').reject(&:blank?)

  # find page details using find_by_slug method and assign it to a variable
  page = Page.find_by_slug(split_path.shift)

  # find child page and then details of page using find_by_slug method and assign it to a variable
  page = page.children.find_by_slug(split_path.shift) until page.nil? || split_path.empty?

  # return page variable
  page

end

.find_by_slug_or_id(path, id) ⇒ Object

Helps to resolve the situation where you have a path and an id If page corresponding to path is not found then try finding page by id.

Parameters:

  • =

    integer

Returns:

  • Array



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/page.rb', line 81

def self.find_by_slug_or_id(path, id)

  # if path is present
  if path.present?

    # then call find_by_path method to find the page
    find_by_path(path)

  #if path is not present but ID is present,
  elsif id.present?

    # then call rails find method to find page by id
    find_by_path(id)

  end # end outer if

end

Instance Method Details

#deletable?Boolean

Returns : boolean.

Returns:

  • (Boolean)

    : boolean



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/page.rb', line 123

def deletable?

  # if link url is / or /404 then page is not deletable
  if link_url == '/' || link_url == '/404'

    false

  else

    true

  end # end if

end

#nested_urlObject

Returns : none.

Parameters:

  • :

    none

Returns:

  • : none



141
142
143
144
145
146
# File 'app/models/page.rb', line 141

def nested_url

  # find the parent of the current page and convert the current page slug to nested url
  [parent, to_param.to_s].compact.flatten

end