Class: Kuhsaft::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Kuhsaft::Page
- Defined in:
- app/models/kuhsaft/page.rb
Class Method Summary collapse
- .current_translation_locale ⇒ Object
- .current_translation_locale=(locale) ⇒ Object
- .find_by_url(url) ⇒ Object
- .position_of(id) ⇒ Object
- .translation_locales ⇒ Object
- .translation_locales=(array) ⇒ Object
Instance Method Summary collapse
- #decrement_position ⇒ Object
- #increment_position ⇒ Object
- #link ⇒ Object
- #position_to_top ⇒ Object
- #preceding_sibling ⇒ Object
- #preceding_siblings ⇒ Object
- #recount_siblings_position_from(position) ⇒ Object
- #reposition(before_id) ⇒ Object
- #root? ⇒ Boolean
- #save_translation ⇒ Object
- #set_position ⇒ Object
- #siblings ⇒ Object
- #succeeding_sibling ⇒ Object
- #succeeding_siblings ⇒ Object
- #translation ⇒ Object
Class Method Details
.current_translation_locale ⇒ Object
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_locales ⇒ Object
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_position ⇒ Object
45 46 47 |
# File 'app/models/kuhsaft/page.rb', line 45 def decrement_position update_attribute :position, position - 1 end |
#increment_position ⇒ Object
41 42 43 |
# File 'app/models/kuhsaft/page.rb', line 41 def increment_position update_attribute :position, position + 1 end |
#link ⇒ Object
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_top ⇒ Object
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_sibling ⇒ Object
53 54 55 |
# File 'app/models/kuhsaft/page.rb', line 53 def preceding_sibling siblings.where('position = ?', position - 1).first end |
#preceding_siblings ⇒ Object
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
22 23 24 |
# File 'app/models/kuhsaft/page.rb', line 22 def root? parent.nil? end |
#save_translation ⇒ Object
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_position ⇒ Object
88 89 90 |
# File 'app/models/kuhsaft/page.rb', line 88 def set_position update_attribute(:position, siblings.count + 1) end |
#siblings ⇒ Object
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_sibling ⇒ Object
57 58 59 |
# File 'app/models/kuhsaft/page.rb', line 57 def succeeding_sibling siblings.where('position = ?', position + 1).first end |
#succeeding_siblings ⇒ Object
65 66 67 |
# File 'app/models/kuhsaft/page.rb', line 65 def succeeding_siblings siblings.where('position >= ?', position).where('id != ?', id) end |
#translation ⇒ Object
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 |