Allow binding goal saving w/o exposures and disallow destruction
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
module Concerns::Finders
|
||||
private
|
||||
|
||||
def find_goal(id = params[:id])
|
||||
@goal = Goal.find(id)
|
||||
@project = @goal.project
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
def find_meal
|
||||
@meal = Meal.find(params[:id])
|
||||
@project = @meal.project
|
||||
|
||||
@@ -22,9 +22,9 @@ class TargetsController < ApplicationController
|
||||
end
|
||||
|
||||
def create
|
||||
goal = @project.goals.binding if params[:goal][:id].blank?
|
||||
goal ||= @project.goals.find_by(id: params[:goal][:id])
|
||||
goal ||= @project.goals.build(goal_params)
|
||||
goal_id = params[:goal][:id]
|
||||
goal = goal_id.present? ? @project.goals.find(goal_id) : @project.goals.new
|
||||
goal.attributes = goal_params unless goal.is_binding?
|
||||
|
||||
@targets = goal.targets.build(targets_params[:targets]) do |target|
|
||||
target.effective_from = params[:target][:effective_from]
|
||||
@@ -70,7 +70,7 @@ class TargetsController < ApplicationController
|
||||
private
|
||||
|
||||
def goal_params
|
||||
params.require(:goal).permit(:id, :name, :description)
|
||||
params.require(:goal).permit(:name, :description)
|
||||
end
|
||||
|
||||
def targets_params
|
||||
|
||||
@@ -6,7 +6,7 @@ class Goal < ActiveRecord::Base
|
||||
class_name: 'Exposure', extend: BodyTracking::TogglableExposures
|
||||
has_many :quantities, -> { order "lft" }, through: :target_exposures
|
||||
|
||||
validates :target_exposures, presence: true
|
||||
validates :target_exposures, presence: true, unless: :is_binding?
|
||||
validates :is_binding, uniqueness: {scope: :project_id}, if: :is_binding?
|
||||
validates :name, presence: true, uniqueness: {scope: :project_id},
|
||||
exclusion: {in: [I18n.t('targets.form.binding_goal')], unless: :is_binding?}
|
||||
@@ -16,4 +16,7 @@ class Goal < ActiveRecord::Base
|
||||
self.is_binding = false if self.is_binding.nil?
|
||||
end
|
||||
end
|
||||
before_destroy prepend: true do
|
||||
!is_binding?
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user