virtual_attribute 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,8 +4,9 @@ Adds a virtual attribute to models, that plays nice with forms etc
4
4
  example:
5
5
 
6
6
  class MyModel < ActiveRecord::Base
7
- virtual_attribute :foo, :boolean => true, :attr_type => :attr_accessible
8
- virtual_attribute :bar, :boolean => true, :attr_type => :attr_accessible
7
+ virtual_attribute :foo, :type => :boolean, :attr_type => :attr_accessible
8
+ virtual_attribute :bar, :type => :boolean, :attr_type => :attr_accessible
9
+ virtual_attribute :dated_on, :type => :date, :attr_type => :attr_accessible
9
10
  end
10
11
 
11
12
  params = {:foo => '1', :bar => 'false'}
@@ -15,13 +16,16 @@ example:
15
16
  thing.bar #=> false
16
17
 
17
18
 
18
- Virtual attributes can be set to :boolean => true
19
-
19
+ Virtual attributes can be set to :type => :boolean
20
20
  - This will parse ['yes', 'true', '1', 1] to be true
21
21
  - and ['no', 'false', '0', 0] to be false
22
22
  - otherwise will be nil
23
23
  - this allows them to be used transparently with checkboxes etc in forms
24
24
 
25
+ Virtual attributes can be set to :type => :date, :type => :time, :type => :datetime
26
+ - This allows them to be used with date_select etc helpers in rails forms
27
+
28
+
25
29
  If using attr_accessible in your model, the virtual attribute should be added to the list to, or you can pass:
26
30
 
27
31
  :attr_type => :attr_accessible
@@ -1,3 +1,3 @@
1
1
  module VirtualAttribute
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -4,7 +4,7 @@ class Module
4
4
  inst_var = "@#{name.to_s}"
5
5
  module_eval do
6
6
 
7
- if options[:boolean]
7
+ if options[:boolean] or (options[:type] == :boolean)
8
8
  define_method(name_equals) do |val|
9
9
  val = val.to_s.strip
10
10
  if ['1', 'true', 'yes'].include?(val)
@@ -13,12 +13,32 @@ class Module
13
13
  instance_variable_set(inst_var, false)
14
14
  end
15
15
  end
16
+ elsif (options[:type] == :float)
17
+ define_method(name_equals) do |val|
18
+ begin
19
+ instance_variable_set(inst_var, Float(val))
20
+ rescue ArgumentError
21
+ instance_variable_set(inst_var, nil)
22
+ end
23
+ end
24
+ elsif (options[:type] == :integer)
25
+ define_method(name_equals) do |val|
26
+ begin
27
+ instance_variable_set(inst_var, Integer(val))
28
+ rescue ArgumentError
29
+ instance_variable_set(inst_var, nil)
30
+ end
31
+ end
16
32
  else
17
33
  define_method(name_equals) do |val|
18
34
  instance_variable_set(inst_var, val)
19
35
  end
20
36
  end
21
37
 
38
+ if [:date, :time, :datetime].include? options[:type]
39
+ columns_hash[name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, nil, options[:type].to_s)
40
+ end
41
+
22
42
  define_method(name) do
23
43
  instance_variable_get(inst_var)
24
44
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 3
9
- version: 0.0.3
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Philip Roberts
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-10 00:00:00 +01:00
17
+ date: 2010-09-10 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies: []
20
20