Skip to content

Commit 782d3a3

Browse files
authored
Separate memorable_date into separate file (#66)
1 parent b1675cf commit 782d3a3

2 files changed

Lines changed: 59 additions & 50 deletions

File tree

app/lib/flex/attributes.rb

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,17 @@
11
module Flex
22
module Attributes
33
extend ActiveSupport::Concern
4-
5-
# A custom ActiveRecord type that allows storing a date. It behaviors the same
6-
# as the default Date type, but it allows setting the attribute using a hash
7-
# with keys :year, :month, and :day. This is meant to be used in conjunction
8-
# with the memorable_date method in the Flex form builder
9-
class MemorableDate < ActiveRecord::Type::Date
10-
# Override cast to allow setting the date via a Hash with keys :year, :month, :day.
11-
def cast(value)
12-
return super(value) unless value.is_a?(Hash)
13-
14-
begin
15-
# Use strptime since Date.new is too lenient, allowing things like negative months and years
16-
value = Date.strptime("#{value[:year]}-#{value[:month]}-#{value[:day]}", "%Y-%m-%d")
17-
rescue ArgumentError
18-
nil
19-
end
20-
end
21-
22-
def type
23-
:date_from_hash
24-
end
25-
end
4+
include Flex::Attributes::MemorableDateAttribute
265

276
class_methods do
287
def flex_attribute(name, type, options = {})
29-
if type == :memorable_date
8+
case type
9+
when :memorable_date
3010
memorable_date_attribute name, options
3111
else
3212
raise ArgumentError, "Unsupported attribute type: #{type}"
3313
end
3414
end
35-
36-
private
37-
38-
def memorable_date_attribute(name, options)
39-
attribute name, MemorableDate.new
40-
41-
validate :"validate_#{name}"
42-
43-
if options[:presence]
44-
validates name, presence: true
45-
end
46-
47-
# Create a validation method that checks if the value is a valid date
48-
define_method "validate_#{name}" do
49-
value = send(name)
50-
raw_value = read_attribute_before_type_cast(name)
51-
52-
# If model.<attribute> is nil, but model.<attribute>_before_type_cast is not nil,
53-
# that means the application failed to cast the value to the appropriate type in
54-
# order to complete the attribute assignment. This means the original value
55-
# is invalid.
56-
did_type_cast_fail = value.nil? && raw_value.present?
57-
if did_type_cast_fail
58-
errors.add(name, :invalid_date)
59-
end
60-
end
61-
end
6215
end
6316
end
6417
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module Flex
2+
module Attributes
3+
module MemorableDateAttribute
4+
extend ActiveSupport::Concern
5+
6+
# A custom ActiveRecord type that allows storing a date. It behaviors the same
7+
# as the default Date type, but it allows setting the attribute using a hash
8+
# with keys :year, :month, and :day. This is meant to be used in conjunction
9+
# with the memorable_date method in the Flex form builder
10+
class MemorableDate < ActiveRecord::Type::Date
11+
# Override cast to allow setting the date via a Hash with keys :year, :month, :day.
12+
def cast(value)
13+
return super(value) unless value.is_a?(Hash)
14+
15+
begin
16+
# Use strptime since Date.new is too lenient, allowing things like negative months and years
17+
value = Date.strptime("#{value[:year]}-#{value[:month]}-#{value[:day]}", "%Y-%m-%d")
18+
rescue ArgumentError
19+
nil
20+
end
21+
end
22+
23+
def type
24+
:date_from_hash
25+
end
26+
end
27+
28+
class_methods do
29+
def memorable_date_attribute(name, options)
30+
attribute name, MemorableDate.new
31+
32+
validate :"validate_#{name}"
33+
34+
if options[:presence]
35+
validates name, presence: true
36+
end
37+
38+
# Create a validation method that checks if the value is a valid date
39+
define_method "validate_#{name}" do
40+
value = send(name)
41+
raw_value = read_attribute_before_type_cast(name)
42+
43+
# If model.<attribute> is nil, but model.<attribute>_before_type_cast is not nil,
44+
# that means the application failed to cast the value to the appropriate type in
45+
# order to complete the attribute assignment. This means the original value
46+
# is invalid.
47+
did_type_cast_fail = value.nil? && raw_value.present?
48+
if did_type_cast_fail
49+
errors.add(name, :invalid_date)
50+
end
51+
end
52+
end
53+
end
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)