diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/calendars_controller.rb | 1 | ||||
| -rw-r--r-- | app/models/calendar.rb | 97 | ||||
| -rw-r--r-- | app/models/calendar/date_value.rb | 38 | ||||
| -rw-r--r-- | app/models/calendar/period.rb | 60 |
4 files changed, 102 insertions, 94 deletions
diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index 86d567882..a2bfe9b63 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -5,6 +5,7 @@ class CalendarsController < BreadcrumbController respond_to :html respond_to :js, only: :index + private def calendar_params permitted_params = [:id, :name, :short_name, periods_attributes: [:id, :begin, :end, :_destroy], date_values_attributes: [:id, :value, :_destroy]] diff --git a/app/models/calendar.rb b/app/models/calendar.rb index 39e0d6d49..215b13bb7 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -30,66 +30,6 @@ class Calendar < ActiveRecord::Base end end - class Period - include ActiveAttr::Model - - attribute :id, type: Integer - attribute :begin, type: Date - attribute :end, type: Date - - validates_presence_of :begin, :end - validate :check_end_greather_than_begin - - def check_end_greather_than_begin - if self.begin and self.end and self.begin > self.end - errors.add(:end, :invalid) - end - end - - def self.from_range(index, range) - Period.new id: index, begin: range.begin, end: range.end - end - - def range - if self.begin and self.end and self.begin <= self.end - Range.new self.begin, self.end - end - end - - def intersect?(*other) - return false if range.nil? - - other = other.flatten - other = other.delete_if { |o| o.id == id } if id - - other.any? do |period| - if other_range = period.range - (range & other_range).present? - end - end - end - - def cover? date - range.cover? date - end - - # Stuff required for coocon - def new_record? - !persisted? - end - - def persisted? - id.present? - end - - def mark_for_destruction - self._destroy = true - end - - attribute :_destroy, type: Boolean - alias_method :marked_for_destruction?, :_destroy - end - # Required by coocon def build_period Period.new @@ -132,6 +72,8 @@ class Calendar < ActiveRecord::Base def flatten_date_array attributes, key date_int = %w(1 2 3).map {|e| attributes["#{key}(#{e}i)"].to_i } Date.new(*date_int) + rescue + nil end def periods_attributes=(attributes = {}) @@ -166,35 +108,6 @@ class Calendar < ActiveRecord::Base ### dates - class DateValue - include ActiveAttr::Model - - attribute :id, type: Integer - attribute :value, type: Date - - validates_presence_of :value - - def self.from_date(index, date) - DateValue.new id: index, value: date - end - - # Stuff required for coocon - def new_record? - !persisted? - end - - def persisted? - id.present? - end - - def mark_for_destruction - self._destroy = true - end - - attribute :_destroy, type: Boolean - alias_method :marked_for_destruction?, :_destroy - end - # Required by coocon def build_date_value DateValue.new @@ -216,11 +129,7 @@ class Calendar < ActiveRecord::Base validate :validate_date_values def validate_date_values - date_values_are_valid = true - - unless date_values.all?(&:valid?) - date_values_are_valid = false - end + date_values_are_valid = date_values.all?(&:valid?) date_values.each do |date_value| if date_values.count { |d| d.value == date_value.value } > 1 diff --git a/app/models/calendar/date_value.rb b/app/models/calendar/date_value.rb new file mode 100644 index 000000000..9dfa7e5f3 --- /dev/null +++ b/app/models/calendar/date_value.rb @@ -0,0 +1,38 @@ +class Calendar::DateValue + include ActiveAttr::Model + + attribute :id, type: Integer + attribute :value, type: Date + + validate :validate + + def initialize(atts={}) + super( atts.slice(:id, :value) ) + # date stored for error message + @date = atts.slice(*%w{value(3i) value(2i) value(1i)}).values.join("/") + end + + def self.from_date(index, date) + DateValue.new id: index, value: date + end + + # Stuff required for coocon + def new_record? + !persisted? + end + + def persisted? + id.present? + end + + def mark_for_destruction + self._destroy = true + end + + def validate + errors.add(:value, "Illegal Date #{@date}") if value.blank? + end + + attribute :_destroy, type: Boolean + alias_method :marked_for_destruction?, :_destroy +end diff --git a/app/models/calendar/period.rb b/app/models/calendar/period.rb new file mode 100644 index 000000000..0f1902e59 --- /dev/null +++ b/app/models/calendar/period.rb @@ -0,0 +1,60 @@ +class Calendar::Period + include ActiveAttr::Model + + attribute :id, type: Integer + attribute :begin, type: Date + attribute :end, type: Date + + validates_presence_of :begin, :end + validate :check_end_greather_than_begin + + def check_end_greather_than_begin + if self.begin and self.end and self.begin > self.end + errors.add(:end, :invalid) + end + end + + def self.from_range(index, range) + Period.new id: index, begin: range.begin, end: range.end + end + + def range + if self.begin and self.end and self.begin <= self.end + Range.new self.begin, self.end + end + end + + def intersect?(*other) + return false if range.nil? + + other = other.flatten + other = other.delete_if { |o| o.id == id } if id + + other.any? do |period| + if other_range = period.range + (range & other_range).present? + end + end + end + + def cover? date + range.cover? date + end + + # Stuff required for coocon + def new_record? + !persisted? + end + + def persisted? + id.present? + end + + def mark_for_destruction + self._destroy = true + end + + attribute :_destroy, type: Boolean + alias_method :marked_for_destruction?, :_destroy +end + |
