Skip to content

Commit

Permalink
Adding clean-up of conditions on associations to use where with `…
Browse files Browse the repository at this point in the history
…->` for Rails 4.0 compatibility
  • Loading branch information
rylanb committed Feb 24, 2019
1 parent 66f681f commit 9895afd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 24 deletions.
12 changes: 5 additions & 7 deletions app/models/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ class Log < ActiveRecord::Base
belongs_to :region

has_many :log_volunteers
has_many :volunteers, through: :log_volunteers,
conditions: {'log_volunteers.active' => true}
has_many :inactive_volunteers, through: :log_volunteers,
conditions: {'log_volunteers.active' => false}
has_many :volunteers, -> { where(log_volunteers: { active: true }) }, through: :log_volunteers
has_many :inactive_volunteers, -> { where(log_volunteers: { active: false }) }, through: :log_volunteers
has_many :log_recipients
has_many :recipients, through: :log_recipients
has_many :log_parts
Expand All @@ -27,14 +25,14 @@ class Log < ActiveRecord::Base
accepts_nested_attributes_for :log_volunteers
accepts_nested_attributes_for :schedule_chain

validates :notes, presence: { if: Proc.new{ |a| a.complete and a.summed_weight == 0 and a.summed_count == 0 and a.why_zero == 2 },
validates :notes, presence: { if: Proc.new{ |a| a.complete && a.summed_weight == 0 && a.summed_count == 0 && a.why_zero == 2 },
message: "can't be blank if weights/counts are all zero: let us know what happened!" }
validates :transport_type_id, presence: { if: :complete }
validates :donor_id, presence: { if: :complete }
validates :scale_type_id, presence: { if: :complete }
validates :when, presence: true
validates :hours_spent, presence: { if: :complete }
validates :why_zero, presence: { if: Proc.new{ |a| a.complete and a.summed_weight == 0 and a.summed_count == 0 } }
validates :why_zero, presence: { if: Proc.new{ |a| a.complete && a.summed_weight == 0 && a.summed_count == 0 } }

attr_accessible :region_id, :donor_id, :why_zero,
:food_type_id, :transport_type_id, :flag_for_admin, :notes,
Expand All @@ -46,7 +44,7 @@ class Log < ActiveRecord::Base
# units conversion on scale type --- we always store in lbs in the database
before_save { |record|
return if record.region.nil?
record.scale_type = record.region.scale_types.first if record.scale_type.nil? and record.region.scale_types.length == 1
record.scale_type = record.region.scale_types.first if record.scale_type.nil? && record.region.scale_types.length == 1
unless record.scale_type.nil?
record.weight_unit = record.scale_type.weight_unit if record.weight_unit.nil?
record.log_parts.each{ |lp|
Expand Down
2 changes: 1 addition & 1 deletion app/models/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

class Schedule < ActiveRecord::Base
include RankedModel
default_scope { order(position: :asc) }

has_many :logs

belongs_to :location
belongs_to :schedule_chain
ranks :position, with_same: :schedule_chain_id
default_scope order('position ASC')

has_many :schedule_parts
has_many :food_types, through: :schedule_parts
Expand Down
3 changes: 1 addition & 2 deletions app/models/schedule_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class ScheduleChain < ActiveRecord::Base
belongs_to :region

has_many :schedule_volunteers
has_many :volunteers, through: :schedule_volunteers,
conditions: { 'schedule_volunteers.active' => true }
has_many :volunteers, -> { where(schedule_volunteers: { active: true }) }, through: :schedule_volunteers

has_many :schedules
has_many :locations, through: :schedules
Expand Down
1 change: 1 addition & 0 deletions app/models/schedule_volunteer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class ScheduleVolunteer < ActiveRecord::Base
attr_accessible :schedule_chain_id, :volunteer_id, :active

accepts_nested_attributes_for :volunteer
scope :active, -> { where(active: true) }
end
19 changes: 5 additions & 14 deletions app/models/volunteer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,13 @@ class Volunteer < ActiveRecord::Base
has_many :regions, through: :assignments

has_many :schedule_volunteers
has_many :schedule_chains, through: :schedule_volunteers,
conditions: { 'schedule_volunteers.active' => true }
has_many :prior_schedules, through: :schedule_volunteers,
conditions: { 'schedule_volunteers.active' => false },
class_name: 'ScheduleChain'
has_many :schedule_chains, -> { where(schedule_volunteers: { active: true }) }, through: :schedule_volunteers
has_many :prior_schedules, -> { where(schedule_volunteers: { active: false }) }, through: :schedule_volunteers, class_name: 'ScheduleChain'

has_many :log_volunteers
has_many :logs, through: :log_volunteers,
conditions: { log_volunteers: { active: true } }
has_many :completed_logs, through: :log_volunteers,
conditions: { logs: { complete: true } },
class_name: 'Log', source: :log

has_many :prior_logs, through: :log_volunteers,
conditions: { 'log_volunteers.active' => false },
class_name: 'Log'
has_many :logs, -> { where(log_volunteers: { active: true }) }, through: :log_volunteers
has_many :completed_logs, -> { where(logs: { complete: true }) }, through: :log_volunteers, class_name: 'Log', source: :log
has_many :prior_logs, -> { where(log_volunteers: { active: false }) }, through: :log_volunteers, class_name: 'Log', source: :log

attr_accessible :pre_reminders_too, :region_ids, :password,
:password_confirmation, :remember_me, :admin_notes, :email,
Expand Down

0 comments on commit 9895afd

Please sign in to comment.