From 233a8d6e3ed4bb7cd828e21150a4141a93ae9534 Mon Sep 17 00:00:00 2001 From: Matthias Viehweger Date: Mon, 6 Jun 2011 01:08:28 +0200 Subject: [PATCH] some integration work with form-helpers apparently, form.checkbox :foo wants form.object.foo to return 0 or 1 to determine "checked"-state. the return-value of foo? is not taken into account. --- lib/date_checkbox/has_date_checkbox.rb | 22 +++++++++++++++------- test/has_date_checkbox_test.rb | 6 ++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/date_checkbox/has_date_checkbox.rb b/lib/date_checkbox/has_date_checkbox.rb index 617eb1d..a8e8da3 100644 --- a/lib/date_checkbox/has_date_checkbox.rb +++ b/lib/date_checkbox/has_date_checkbox.rb @@ -6,26 +6,34 @@ def self.included(base) module ClassMethods def has_date_checkbox(db_field) - method_name = db_field.to_s.sub(/_at$/, '') + # if db_field.to_s =~ /_at$/ #&& has_attribute?(db_field) + column_name = db_field + method_name = db_field.to_s.sub(/_at$/, '') + # elsif has_attribute?(:"#{db_field}_at") + # else + # column_name = :"#{db_field}_at" + # method_name = db_field.to_s + # end + # send :include, InstanceMethods class_eval do define_method("#{method_name}") do - if attribute_present?(db_field) - I18n.t(:"is_#{method_name}") + if attribute_present?(column_name) + "1" # I18n.t(:"is_#{method_name}") else - I18n.t(:"not_#{method_name}") + "0" # I18n.t(:"not_#{method_name}") end end define_method("#{method_name}?") do - attribute_present?(db_field) + attribute_present?(column_name) end define_method("#{method_name}=") do |value| if value.to_s == "0" - write_attribute(db_field, nil) + write_attribute(column_name, nil) else - write_attribute(db_field, Time.now) + write_attribute(column_name, Time.now) end end end diff --git a/test/has_date_checkbox_test.rb b/test/has_date_checkbox_test.rb index acdb047..51246ae 100644 --- a/test/has_date_checkbox_test.rb +++ b/test/has_date_checkbox_test.rb @@ -19,13 +19,15 @@ def teardown test "returns something falsish when attribute is not set" do instance.published_at = nil assert_equal false, instance.published? - assert_equal I18n.t(:not_published), instance.published + assert_equal '0', instance.published + # assert_equal I18n.t(:not_published), instance.published end test "returns something trueish when attribute is set" do instance.published_at = 1.minute.ago assert_equal true, instance.published? - assert_equal I18n.t(:is_published), instance.published + assert_equal '1', instance.published + # assert_equal I18n.t(:is_published), instance.published end test "has_date_checkbox defines a custom writer for the given attribute" do