Permalink
Browse files

Initial commit

  • Loading branch information...
binarylogic committed Sep 11, 2008
0 parents commit 9c022f2b5e5ff129ef0442328b38de0bf614716a
Showing with 72 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +14 −0 README.mdown
  3. +1 −0 init.rb
  4. +37 −0 lib/boolean_datetime_attribute.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Ben Johnson of Binary Logic (binarylogic.com)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
+# Boolean datetime attribute
+
+## What is boolean attribute?
+
+This plugin allows you to replace boolean columns with datetime columns. For example, you have a User model with the "inactive" boolean column. Why not make it more informative and change that column to "inactive_at"? Now you know when the user was marked inactive. This plugin allows you to do that and still get all of the methods that you would have for a boolean column. It's transparent, you can treat it as a boolean, but it will store it as a date.
+
+## How it works
+
+ class User < ActiveRecord::Base
+ boolean_datetime_attribute :inactive_at
+ end
+
+
+Copyright (c) 2007 Ben Johnson of [Binary Logic](http://www.binarylogic.com), released under the MIT license
@@ -0,0 +1 @@
+require "boolean_datetime_attribute"
@@ -0,0 +1,37 @@
+module BinaryLogic
+ module BooleanDatetimeAttribute
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def boolean_datetime_attribute(*datetime_fields)
+ datetime_fields.each do |field|
+ boolean_field = field.to_s.gsub(/_at$/, "")
+ src = "def #{boolean_field}=(value)
+ if (value == true || value == 'true' || value == 1 || value == '1')
+ self.#{field} = Time.now if !#{boolean_field}?
+ else
+ self.#{field} = nil if #{boolean_field}?
+ end
+ #{field}
+ end
+
+ def #{boolean_field}?
+ !#{field}.blank? && #{field} <= Time.current
+ end
+ alias_method :#{boolean_field}, :#{boolean_field}?
+
+ def #{boolean_field}!
+ update_attribute :#{field}, Time.now
+ end
+ "
+
+ class_eval src
+ end
+ end
+ end
+ end
+end
+
+ActiveRecord::Base.send(:include, BinaryLogic::BooleanDatetimeAttribute)

0 comments on commit 9c022f2

Please sign in to comment.