Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/serial_preference/preference_definition.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SerialPreference
class PreferenceDefinition

SUPPORTED_TYPES = [:string,:integer,:decimal,:float,:boolean]
SUPPORTED_TYPES = [:string,:integer,:decimal,:float,:boolean,:date]

attr_accessor :data_type, :name, :default, :required, :field_type

Expand Down Expand Up @@ -61,7 +61,7 @@ def value(v)
case data_type
when :string, :password
v.to_s
when :integer
when :integer
v.respond_to?(:to_i) ? v.to_i : nil
when :float, :real
v.respond_to?(:to_f) ? v.to_f : nil
Expand All @@ -73,11 +73,13 @@ def value(v)
return false if v == "0"
return false if v.to_s.downcase == "no"
!!v
when :date
v.respond_to?(:to_date) ? v.to_date : nil
else
nil
end
end
end

end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/dummy_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class DummyClass < ActiveRecord::Base
preference :required_number, data_type: :integer, required: :true
preference :vat_no, required: false
preference :max_invoice_items, data_type: :integer
preference :shipping_date, data_type: :date
preference_group "Preferred Ledgers" do
income_ledger_id data_type: :integer, default: 1
creditable data_type: :boolean, default: false
Expand Down
5 changes: 2 additions & 3 deletions spec/has_preference_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class OverriddenPreferenceAttributeClass < ActiveRecord::Base
end
end


context "should define accessors" do
it "should have readers available" do
d = DummyClass.new
expect(d.respond_to?(:taxable)).to be_truthy
expect(d.respond_to?(:vat_no)).to be_truthy
expect(d.respond_to?(:max_invoice_items)).to be_truthy
expect(d.respond_to?(:income_ledger_id)).to be_truthy
expect(d.respond_to?(:shipping_date)).to be_truthy
end

it "should ensure that the readers returns the correct data" do
Expand All @@ -57,6 +57,7 @@ class OverriddenPreferenceAttributeClass < ActiveRecord::Base
expect(d.respond_to?(:vat_no=)).to be_truthy
expect(d.respond_to?(:max_invoice_items=)).to be_truthy
expect(d.respond_to?(:income_ledger_id=)).to be_truthy
expect(d.respond_to?(:shipping_date=)).to be_truthy
end

it "should ensure that the writer write the correct data" do
Expand Down Expand Up @@ -89,8 +90,6 @@ class OverriddenPreferenceAttributeClass < ActiveRecord::Base

end



=begin
context "should define validations" do
it "should define presence validation on required preferences" do
Expand Down
24 changes: 23 additions & 1 deletion spec/preference_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
end

it "should be not numerical when data_type is non numerical" do
[:string,:boolean,:password,:whatever].each do |dt|
[:string,:boolean,:password,:whatever,:date].each do |dt|
expect(described_class.new("whatever",{data_type: dt})).to_not be_numerical
end
end
Expand Down Expand Up @@ -179,6 +179,28 @@
end
end

context "should report correct date values" do
before do
@preference = described_class.new("birthday",{data_type: :date})
end
it "should return correct dates" do
["", "2014-08-01", "2014-08-01 14:00"].each do |input_val|
expect(@preference.value(input_val)).to eq(input_val.to_date)
end
end

it "should report default date when input is nil" do
p = described_class.new("birthday",{data_type: :date,default: "1990-05-08"})
expect(p.value(nil)).to eq(Date.parse("1990-05-08"))
end

it "should report nil for non date input" do
[[],{}].each do |input_val|
expect(@preference.value(input_val)).to be_nil
end
end
end

end

end