Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add a formhelper for date_checkbox

  • Loading branch information...
commit fe7e0f0406a93ca8096ecfcd06e3addfb338ff4a 1 parent 233a8d6
@kronn authored
View
2  lib/date_checkbox.rb
@@ -1,5 +1,7 @@
module DateCheckbox
autoload :HasDateCheckbox, 'date_checkbox/has_date_checkbox'
+ autoload :FormHelper, 'date_checkbox/form_helper'
end
ActiveRecord::Base.send(:include, DateCheckbox::HasDateCheckbox)
+ActionView::Helpers::FormBuilder.send(:include, DateCheckbox::FormHelper)
View
12 lib/date_checkbox/form_helper.rb
@@ -0,0 +1,12 @@
+module DateCheckbox
+ module FormHelper
+ def date_checkbox(attribute, options = {})
+ long, short = ( attribute.to_s =~ /_at$/ ) ?
+ [ attribute, attribute.to_s.sub(/_at$/, '') ] :
+ [ :"#{attribute}_at", attribute ]
+
+ info = object.send(long).to_date if object.send(:"#{short}?")
+ check_box(short) + info.to_s
+ end
+ end
+end
View
49 test/form_helper_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class DateCheckboxFormHelperTest < ActiveSupport::TestCase
+ test "form-helpers include the the date_checkbox-helper" do
+ assert_respond_to instance, :date_checkbox
+ assert_equal -2, instance.method(:date_checkbox).arity
+ end
+
+ test "helper outputs a checkbox" do
+ assert_nothing_raised(Exception) do
+ result = instance.date_checkbox :published_at
+ assert_equal "check_box published", result
+
+ result = instance.date_checkbox :published
+ assert_equal "check_box published", result
+ end
+ end
+
+ test "helper output the info after the checkbox" do
+ assert_nothing_raised(Exception) do
+ instance = instance(:published_at => Time.now)
+ result = instance.date_checkbox :published_at
+ assert_match(/^check_box published\d{4}-\d{2}-\d{2}$/, result)
+
+ result = instance.date_checkbox :published
+ assert_match(/^check_box published\d{4}-\d{2}-\d{2}$/, result)
+ end
+ end
+
+ def klass
+ @klass ||= Class.new() do
+ include DateCheckbox::FormHelper
+
+ attr_reader :object
+
+ def initialize(object_args = {})
+ @object = PublishedAtPost.new(object_args)
+ end
+
+ def check_box(sym)
+ "check_box #{sym}"
+ end
+ end
+ end
+
+ def instance(object_args = {})
+ @instance ||= klass.new(object_args)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.