Module: Parted

Included in:
GuideEdition, ProgrammeEdition, TravelAdviceEdition
Defined in:
app/models/parted.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
# File 'app/models/parted.rb', line 4

def self.included(klass)
  klass.embeds_many :parts
  klass.accepts_nested_attributes_for :parts, allow_destroy: true,
    reject_if: proc { |attrs| attrs["title"].blank? and attrs["body"].blank? }
  klass.after_validation :merge_embedded_parts_errors
end

Instance Method Details

#build_clone(target_class = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/parted.rb', line 11

def build_clone(target_class=nil)
  new_edition = super

  # If the new edition is of the same type or another type that has parts,
  # copy over the parts from this edition
  if target_class.nil? or target_class.include? Parted
    new_edition.parts = self.parts.map {|p| p.dup }
  end

  new_edition
end

#order_partsObject



23
24
25
26
27
28
# File 'app/models/parted.rb', line 23

def order_parts
  ordered_parts = parts.sort_by { |p| p.order ? p.order : 99999 }
  ordered_parts.each_with_index do |obj, i|
    obj.order = i + 1
  end
end

#whole_bodyObject



30
31
32
# File 'app/models/parted.rb', line 30

def whole_body
  self.parts.in_order.map { |i| %(\# #{i.title}\n\n#{i.body}) }.join("\n\n")
end