Skip to content

Commit

Permalink
Adapting to Rails4 architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
jarl-dk committed Aug 16, 2013
1 parent 7178d70 commit 88bc3e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
7 changes: 4 additions & 3 deletions lib/validates_timeliness/extensions/attribute_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
errors = []
callstack.each do |name, values_with_empty_parameters|
begin
send(name + "=", MultipleAttribute.new(self, name, values_with_empty_parameters))
send("#{name}=", self.class::MultiparameterAttribute.new(self, name, values_with_empty_parameters).read_value)
rescue => ex
values = values_with_empty_parameters.is_a?(Hash) ? values_with_empty_parameters.values : values_with_empty_parameters
errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name} (#{ex.message})", ex, name)
end
end
unless errors.empty?
raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
error_descriptions = errors.map { |ex| ex.message }.join(",")
raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes [#{error_descriptions}]"
end
end

Expand Down
41 changes: 21 additions & 20 deletions lib/validates_timeliness/extensions/multiparameter_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,49 @@ module MultiparameterAttribute

# private

def invalid_multiparameter_date_or_time_as_string(values)
value = [values[0], *values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-")
value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty?
def invalid_multiparameter_date_or_time_as_string(set_values)
value = [set_values[0], *set_values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-")
value += ' ' + set_values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless set_values[3..5].empty?
value
end

def instantiate_time_object_with_timeliness(name, values)
validate_multiparameter_date_values(values) {
instantiate_time_object_without_timeliness(name, values)
def instantiate_time_object_with_timeliness(set_values)
validate_multiparameter_date_values(set_values) {
instantiate_time_object_without_timeliness(set_values)
}
end

def instantiate_date_object(name, values)
validate_multiparameter_date_values(values) {
Date.new(*values)
def instantiate_date_object(set_values)
validate_multiparameter_date_values(set_values) {
Date.new(*set_values)
}
end

# Yield if date values are valid
def validate_multiparameter_date_values(values)
if values[0..2].all?{ |v| v.present? } && Date.valid_civil?(*values[0..2])
def validate_multiparameter_date_values(set_values)
if set_values[0..2].all?{ |v| v.present? } && Date.valid_civil?(*set_values[0..2])
yield
else
invalid_multiparameter_date_or_time_as_string(values)
invalid_multiparameter_date_or_time_as_string(set_values)
end
end

def read_value_with_timeliness(name, values_from_param)
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
values = values_from_param.is_a?(Hash) ? values_from_param.to_a.sort_by(&:first).map(&:last) : values_from_param
def read_value_with_timeliness
@column = object.class.reflect_on_aggregation(name.to_sym) || object.column_for_attribute(name)
klass = column.klass

if values.empty? || values.all?{ |v| v.nil? }
set_values = values.is_a?(Hash) ? values.to_a.sort_by(&:first).map(&:last) : values
if set_values.empty? || set_values.all?{ |v| v.nil? }
nil
elsif klass == Time
instantiate_time_object(name, values)
instantiate_time_object(set_values)
elsif klass == Date
instantiate_date_object(name, values)
instantiate_date_object(set_values)
else
if respond_to?(:read_other_parameter_value)
read_date_parameter_value(name, values_from_param)
read_date_parameter_value(name, values)
else
klass.new(*values)
klass.new(*set_values)
end
end
end
Expand Down

0 comments on commit 88bc3e9

Please sign in to comment.