diff --git a/app/models/log.rb b/app/models/log.rb index d24182a4..a3bc1e9e 100755 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -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 @@ -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, @@ -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| diff --git a/app/models/schedule.rb b/app/models/schedule.rb index c77c934c..b23404cb 100755 --- a/app/models/schedule.rb +++ b/app/models/schedule.rb @@ -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 diff --git a/app/models/schedule_chain.rb b/app/models/schedule_chain.rb index 0b0a06bc..db47cb3f 100755 --- a/app/models/schedule_chain.rb +++ b/app/models/schedule_chain.rb @@ -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 diff --git a/app/models/schedule_volunteer.rb b/app/models/schedule_volunteer.rb index 907fc9b7..9c315f9e 100755 --- a/app/models/schedule_volunteer.rb +++ b/app/models/schedule_volunteer.rb @@ -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 diff --git a/app/models/volunteer.rb b/app/models/volunteer.rb index 5c4c1351..01c42941 100755 --- a/app/models/volunteer.rb +++ b/app/models/volunteer.rb @@ -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,