forked from SpinaCMS/Spina
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensures position is created on a new structure item
- Loading branch information
1 parent
8b4b0e1
commit cd54c68
Showing
2 changed files
with
12 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
module Spina | ||
class StructureItem < ApplicationRecord | ||
before_validation :ensure_position | ||
belongs_to :structure | ||
has_many :structure_parts, dependent: :destroy | ||
|
||
scope :sorted_by_structure, -> { order('position') } | ||
|
||
validates_presence_of :position | ||
accepts_nested_attributes_for :structure_parts, allow_destroy: true | ||
|
||
def content(structure_part) | ||
structure_parts.find_by(name: structure_part).try(:content) | ||
end | ||
|
||
def ensure_position | ||
self.position ||= Time.now.to_i | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters