Class: Gluttonberg::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Content::PageComponents
Defined in:
app/models/gluttonberg/page.rb

Overview

One of the most important model of Gluttonberg. It stores basic meta data about page and have associations with its related models

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_localizationPageLocalization

Returns current localization of page if its not loaded yet then loads default localization

Returns:



63
64
65
# File 'app/models/gluttonberg/page.rb', line 63

def current_localization
  @current_localization
end

#current_user_idObject

Returns the value of attribute current_user_id.



38
39
40
# File 'app/models/gluttonberg/page.rb', line 38

def current_user_id
  @current_user_id
end

#locale_idObject

Returns the value of attribute locale_id.



38
39
40
# File 'app/models/gluttonberg/page.rb', line 38

def locale_id
  @locale_id
end

#paths_need_recachingObject

Returns the value of attribute paths_need_recaching.



38
39
40
# File 'app/models/gluttonberg/page.rb', line 38

def paths_need_recaching
  @paths_need_recaching
end

Class Method Details

.repair_pages_structureObject

Repair Page tree by optimizing ‘position’ column



126
127
128
# File 'app/models/gluttonberg/page.rb', line 126

def self.repair_pages_structure
  PageRepairer.repair_pages_structure
end

Instance Method Details

#collapsed?(current_user) ⇒ Boolean

check if current page is collapsed for current user?

Returns:

  • (Boolean)


141
142
143
# File 'app/models/gluttonberg/page.rb', line 141

def collapsed?(current_user)
  !self.collapsed_pages.find_all{|page| page.user_id == current_user.id}.blank?
end

#duplicateObject

Duplicate current page and draft it.



136
137
138
# File 'app/models/gluttonberg/page.rb', line 136

def duplicate
  PageDuplicate.duplicate(self)
end

#easy_contents(section_name, opts = {}) ⇒ String

Returns Html safe text content or image path depending on content type.

Returns:

  • (String)

    Html safe text content or image path depending on content type



48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/gluttonberg/page.rb', line 48

def easy_contents(section_name, opts = {})
  begin
    prepared_content = nil
    section_name = section_name.to_sym
    load_localization(opts[:locale]) if current_localization.blank?
    content = current_localization.contents.pluck {|c| (c.respond_to?(:parent) && c.parent.section[:name] ==  section_name ) || (c.respond_to?(:section) && c.section[:name] ==  section_name ) }
    prepared_content = _prepare_content(content, opts)
  rescue
  end
  prepared_content
end

#is_public?Boolean

if page does not belongs to group (membership) then its a public page

Returns:

  • (Boolean)


131
132
133
# File 'app/models/gluttonberg/page.rb', line 131

def is_public?
  groups.blank?
end

#load_default_localizationsObject

Load the default localization and set it as current_localization



120
121
122
123
# File 'app/models/gluttonberg/page.rb', line 120

def load_default_localizations
  Gluttonberg::Locale.first_default.id
  self.current_localization = Gluttonberg::PageLocalization.where(:page_id => id , :locale_id => Gluttonberg::Locale.first_default.id).first
end

#load_localization(locale = nil) ⇒ Object

Load the matching localization as specified in the options TODO Write spec for it



111
112
113
114
115
116
117
# File 'app/models/gluttonberg/page.rb', line 111

def load_localization(locale = nil)
  if locale.blank?
     @current_localization = load_default_localizations
  else
    @current_localization = localizations.where("locale_id = ? AND path LIKE ?", locale.id, "#{path}%").first
  end
end

#localized_contentsObject

Just palms off the request for the contents to the current localization



100
101
102
103
104
105
106
# File 'app/models/gluttonberg/page.rb', line 100

def localized_contents
  @contents ||= begin
    Content.content_associations.inject([]) do |memo, assoc|
      memo += send(assoc).all_with_localization(:page_localization_id => current_localization.id)
    end
  end
end

Returns the localized navigation label, or falls back to the localized page name



71
72
73
74
75
76
77
# File 'app/models/gluttonberg/page.rb', line 71

def nav_label
  if current_localization.navigation_label.blank?
    current_localization.name
  else
    current_localization.navigation_label
  end
end

#pathObject

Delegates to the current_localization path



85
86
87
# File 'app/models/gluttonberg/page.rb', line 85

def path
  current_localization.path
end

#paths_need_recaching?Boolean

this method returns true if page path needs to be recalculated.

Returns:

  • (Boolean)


95
96
97
# File 'app/models/gluttonberg/page.rb', line 95

def paths_need_recaching?
  self.paths_need_recaching
end

#public_pathObject

returns public path of current localization



90
91
92
# File 'app/models/gluttonberg/page.rb', line 90

def public_path
  current_localization.public_path
end

#titleObject

Returns the localized title for the page or a default



80
81
82
# File 'app/models/gluttonberg/page.rb', line 80

def title
  current_localization.name
end