Class: Kuhsaft::Page

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_translation_localeObject



118
119
120
# File 'app/models/kuhsaft/page.rb', line 118

def current_translation_locale
  @translation_locale ||= translation_locales.first
end

.current_translation_locale=(locale) ⇒ Object



122
123
124
125
# File 'app/models/kuhsaft/page.rb', line 122

def current_translation_locale=(locale)
  @translation_locale = locale.to_sym
  I18n.locale = @translation_locale if I18n.available_locales.include?(@translation_locale)
end

.find_by_url(url) ⇒ Object



105
106
107
108
# File 'app/models/kuhsaft/page.rb', line 105

def find_by_url url
  translation = Kuhsaft::LocalizedPage.where('url = ?', url)
  translation.present? && translation.first.present? ? translation.first.page : nil
end

.position_of(id) ⇒ Object



101
102
103
# File 'app/models/kuhsaft/page.rb', line 101

def position_of id
  Kuhsaft::Page.find(id).position rescue 1
end

.translation_localesObject



110
111
112
# File 'app/models/kuhsaft/page.rb', line 110

def translation_locales
  @translation_locales
end

.translation_locales=(array) ⇒ Object



114
115
116
# File 'app/models/kuhsaft/page.rb', line 114

def translation_locales=(array)
  @translation_locales = array.map(&:to_sym) if array.class == Array
end

Instance Method Details

#decrement_positionObject



45
46
47
# File 'app/models/kuhsaft/page.rb', line 45

def decrement_position
  update_attribute :position, position - 1
end

#increment_positionObject



41
42
43
# File 'app/models/kuhsaft/page.rb', line 41

def increment_position
  update_attribute :position, position + 1
end


92
93
94
95
96
97
98
# File 'app/models/kuhsaft/page.rb', line 92

def link
  if body.blank? && childs.count > 0
    childs.first.link
  else
    "/#{url}"
  end
end

#position_to_topObject



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

def position_to_top
  update_attribute :position, 1
  recount_siblings_position_from 1
end

#preceding_siblingObject



53
54
55
# File 'app/models/kuhsaft/page.rb', line 53

def preceding_sibling
  siblings.where('position = ?', position - 1).first
end

#preceding_siblingsObject



61
62
63
# File 'app/models/kuhsaft/page.rb', line 61

def preceding_siblings
  siblings.where('position <= ?', position).where('id != ?', id)
end

#recount_siblings_position_from(position) ⇒ Object



74
75
76
77
# File 'app/models/kuhsaft/page.rb', line 74

def recount_siblings_position_from position
  counter = position
  succeeding_siblings.each { |s| counter += 1; s.update_attribute(:position, counter) }
end

#reposition(before_id) ⇒ Object



79
80
81
82
83
84
85
86
# File 'app/models/kuhsaft/page.rb', line 79

def reposition before_id
  if before_id.blank?
    position_to_top
  else
    update_attribute :position, self.class.position_of(before_id) + 1
    recount_siblings_position_from position
  end
end

#root?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/kuhsaft/page.rb', line 22

def root?
  parent.nil?
end

#save_translationObject



34
35
36
37
38
39
# File 'app/models/kuhsaft/page.rb', line 34

def save_translation
  @localized_page.save unless @localized_page.blank?
  childs.each do |child|
    child.translation.save if child.translation.persisted?
  end
end

#set_positionObject



88
89
90
# File 'app/models/kuhsaft/page.rb', line 88

def set_position
  update_attribute(:position, siblings.count + 1)
end

#siblingsObject



49
50
51
# File 'app/models/kuhsaft/page.rb', line 49

def siblings
  (parent.present? ? parent.childs : Kuhsaft::Page.root_pages).where('id != ?', id)
end

#succeeding_siblingObject



57
58
59
# File 'app/models/kuhsaft/page.rb', line 57

def succeeding_sibling
  siblings.where('position = ?', position + 1).first
end

#succeeding_siblingsObject



65
66
67
# File 'app/models/kuhsaft/page.rb', line 65

def succeeding_siblings
  siblings.where('position >= ?', position).where('id != ?', id)
end

#translationObject



26
27
28
29
30
31
32
# File 'app/models/kuhsaft/page.rb', line 26

def translation
  unless @localized_page.present?
    pages = localized_pages.where('locale = ?', Kuhsaft::Page.current_translation_locale)
    @localized_page = pages.first unless pages.blank?
  end
  @localized_page ||= localized_pages.build :locale => Kuhsaft::Page.current_translation_locale
end