Files
grecia/app/models/budget/heading.rb
Bertocq e3d89261a6 Add Sluggable concern and unique validation to Budget, Group and Heading
* What: We need to generate slug on Budget, Group and Heading classes, validating its unique for its scope

* How: Adding a presence and unique validation using Budget always as scope.
2017-07-05 12:33:16 +02:00

28 lines
704 B
Ruby

class Budget
class Heading < ActiveRecord::Base
include Sluggable
belongs_to :group
has_many :investments
validates :group_id, presence: true
validates :name, presence: true, uniqueness: { if: :name_exists_in_budget_headings }
validates :price, presence: true
validates :slug, presence: true, format: /\A[a-z0-9\-_]+\z/
delegate :budget, :budget_id, to: :group, allow_nil: true
scope :order_by_group_name, -> { includes(:group).order('budget_groups.name', 'budget_headings.name') }
def name_scoped_by_group
"#{group.name}: #{name}"
end
def name_exists_in_budget_headings
group.budget.headings.where(name: name).any?
end
end
end