neerajdotname-active_record_no_table 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/PostInstall.txt +0 -0
- data/README.markdown +48 -0
- data/lib/active_record_no_table.rb +160 -0
- metadata +60 -0
data/History.txt
ADDED
data/PostInstall.txt
ADDED
File without changes
|
data/README.markdown
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
== Introduction
|
2
|
+
|
3
|
+
Using ActiveRecord validation utilitites is difficult if you do not have a table for your model. This gem makes it easy to use validation tools even if the model does not have a corresponding table.
|
4
|
+
|
5
|
+
== How to install
|
6
|
+
|
7
|
+
<pre>
|
8
|
+
<code>
|
9
|
+
gem sources -a http://gems.github.com
|
10
|
+
sudo gem install neerajdotname-active_record_no_table
|
11
|
+
</code>
|
12
|
+
</pre>
|
13
|
+
|
14
|
+
<pre>
|
15
|
+
<code>
|
16
|
+
config.gem "neerajdotname-active_record_no_table",
|
17
|
+
:lib => active_record_no_table',
|
18
|
+
:source => 'http://gems.github.com'
|
19
|
+
</code>
|
20
|
+
</pre>
|
21
|
+
|
22
|
+
|
23
|
+
== How to use this gem
|
24
|
+
|
25
|
+
<pre>
|
26
|
+
<code>
|
27
|
+
class User < ActiveRecord::NoTable
|
28
|
+
validates_presence_of :name
|
29
|
+
attr_accessor :name
|
30
|
+
end
|
31
|
+
|
32
|
+
>> user = User.new
|
33
|
+
>> user.valid?
|
34
|
+
>> user.errors.full_messages
|
35
|
+
>> ["Name can't be blank"]
|
36
|
+
</code>
|
37
|
+
</pre>
|
38
|
+
|
39
|
+
|
40
|
+
== Feedback
|
41
|
+
|
42
|
+
Email me: neerajdotname [at] gmail (dot) com
|
43
|
+
|
44
|
+
== License
|
45
|
+
|
46
|
+
MIT
|
47
|
+
|
48
|
+
Copyright (c) 2009 neerajdotname
|
@@ -0,0 +1,160 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
if Rails.version >= "2.3.0"
|
7
|
+
module ActiveRecord
|
8
|
+
class NoTable
|
9
|
+
include Validateable_2_3
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
elsif Rails.version >= '2.2.0'
|
14
|
+
module ActiveRecord
|
15
|
+
class NoTable
|
16
|
+
include Validateable_2_2
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
elsif Rails.version >= '2.1.0'
|
21
|
+
module ActiveRecord
|
22
|
+
class NoTable
|
23
|
+
include Validateable_2_1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
else
|
28
|
+
raise 'this plugin only works with Rails 2.1.x and higher'
|
29
|
+
end
|
30
|
+
|
31
|
+
module Validateable_2_1
|
32
|
+
[:save, :save!, :update_attribute].each{|attr| define_method(attr){}}
|
33
|
+
|
34
|
+
def method_missing(symbol, *params)
|
35
|
+
if(symbol.to_s =~ /(.*)_before_type_cast$/)
|
36
|
+
send($1)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.append_features(base)
|
41
|
+
super
|
42
|
+
base.send(:include, ActiveRecord::Validations)
|
43
|
+
base.extend ClassMethods
|
44
|
+
|
45
|
+
base.send :include, ActiveSupport::Callbacks
|
46
|
+
base.define_callbacks *ActiveRecord::Validations::VALIDATIONS
|
47
|
+
end
|
48
|
+
|
49
|
+
module ClassMethods
|
50
|
+
def human_attribute_name(attr)
|
51
|
+
attr.humanize
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
module Validateable_2_2
|
57
|
+
|
58
|
+
[:save, :save!, :update_attribute].each{|attr| define_method(attr){}}
|
59
|
+
|
60
|
+
def method_missing(symbol, *params)
|
61
|
+
if(symbol.to_s =~ /(.*)_before_type_cast$/)
|
62
|
+
send($1)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.append_features(base)
|
67
|
+
super
|
68
|
+
base.send(:include, ActiveRecord::Validations)
|
69
|
+
base.extend ClassMethods
|
70
|
+
|
71
|
+
base.send :include, ActiveSupport::Callbacks
|
72
|
+
base.define_callbacks *ActiveRecord::Validations::VALIDATIONS
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
module ClassMethods
|
77
|
+
def self_and_descendents_from_active_record
|
78
|
+
klass = self
|
79
|
+
classes = [klass]
|
80
|
+
while klass != klass.base_class
|
81
|
+
classes << klass = klass.superclass
|
82
|
+
end
|
83
|
+
classes
|
84
|
+
rescue
|
85
|
+
[self]
|
86
|
+
end
|
87
|
+
|
88
|
+
def human_name(options = {})
|
89
|
+
defaults = self_and_descendents_from_active_record.map do |klass|
|
90
|
+
:"#{klass.name.underscore}"
|
91
|
+
end
|
92
|
+
defaults << self.name.humanize
|
93
|
+
I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options))
|
94
|
+
end
|
95
|
+
|
96
|
+
def human_attribute_name(attribute_key_name, options = {})
|
97
|
+
defaults = self_and_descendents_from_active_record.map do |klass|
|
98
|
+
:"#{klass.name.underscore}.#{attribute_key_name}"
|
99
|
+
end
|
100
|
+
defaults << options[:default] if options[:default]
|
101
|
+
defaults.flatten!
|
102
|
+
defaults << attribute_key_name.humanize
|
103
|
+
options[:count] ||= 1
|
104
|
+
I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes]))
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
module Validateable_2_3
|
110
|
+
|
111
|
+
[:save, :save!, :update_attribute].each{|attr| define_method(attr){}}
|
112
|
+
|
113
|
+
def method_missing(symbol, *params)
|
114
|
+
if(symbol.to_s =~ /(.*)_before_type_cast$/)
|
115
|
+
send($1)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.append_features(base)
|
120
|
+
super
|
121
|
+
base.send(:include, ActiveRecord::Validations)
|
122
|
+
base.extend ClassMethods
|
123
|
+
|
124
|
+
base.send :include, ActiveSupport::Callbacks
|
125
|
+
base.define_callbacks *ActiveRecord::Validations::VALIDATIONS
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
module ClassMethods
|
130
|
+
def self_and_descendents_from_active_record
|
131
|
+
klass = self
|
132
|
+
classes = [klass]
|
133
|
+
while klass != klass.base_class
|
134
|
+
classes << klass = klass.superclass
|
135
|
+
end
|
136
|
+
classes
|
137
|
+
rescue
|
138
|
+
[self]
|
139
|
+
end
|
140
|
+
|
141
|
+
def human_name(options = {})
|
142
|
+
defaults = self_and_descendents_from_active_record.map do |klass|
|
143
|
+
:"#{klass.name.underscore}"
|
144
|
+
end
|
145
|
+
defaults << self.name.humanize
|
146
|
+
I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options))
|
147
|
+
end
|
148
|
+
|
149
|
+
def human_attribute_name(attribute_key_name, options = {})
|
150
|
+
defaults = self_and_descandants_from_active_record.map do |klass|
|
151
|
+
:"#{klass.name.underscore}.#{attribute_key_name}"
|
152
|
+
end
|
153
|
+
defaults << options[:default] if options[:default]
|
154
|
+
defaults.flatten!
|
155
|
+
defaults << attribute_key_name.humanize
|
156
|
+
options[:count] ||= 1
|
157
|
+
I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes]))
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: neerajdotname-active_record_no_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Neeraj Singh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-30 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: validate non active record model.
|
17
|
+
email:
|
18
|
+
- neerajdotname@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- History.txt
|
25
|
+
- PostInstall.txt
|
26
|
+
- README.markdown
|
27
|
+
files:
|
28
|
+
- History.txt
|
29
|
+
- PostInstall.txt
|
30
|
+
- README.markdown
|
31
|
+
- lib/active_record_no_table.rb
|
32
|
+
has_rdoc: false
|
33
|
+
homepage: http://github.com/neerajdotname/active_record_no_table
|
34
|
+
post_install_message: PostInstall.txt
|
35
|
+
rdoc_options:
|
36
|
+
- --main
|
37
|
+
- README.markdown
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project: neerajdotname
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: validate a non active record model
|
59
|
+
test_files: []
|
60
|
+
|