ohm-contrib 0.0.39 → 0.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ohm/contrib.rb +17 -17
- data/lib/ohm/contrib/active_model_extension.rb +83 -0
- data/test/activemodel_test.rb +25 -0
- metadata +5 -3
data/lib/ohm/contrib.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
module Ohm
|
2
2
|
module Contrib
|
3
|
-
VERSION = "0.0.
|
3
|
+
VERSION = "0.0.40"
|
4
4
|
end
|
5
5
|
|
6
|
-
autoload :
|
7
|
-
autoload :
|
8
|
-
autoload :
|
9
|
-
autoload :
|
10
|
-
autoload :
|
11
|
-
autoload :
|
12
|
-
autoload :
|
13
|
-
autoload :
|
14
|
-
autoload :
|
15
|
-
autoload :
|
16
|
-
autoload :
|
17
|
-
autoload :
|
18
|
-
autoload :
|
19
|
-
autoload :
|
20
|
-
|
21
|
-
|
6
|
+
autoload :ActiveModelExtension, "ohm/contrib/active_model_extension"
|
7
|
+
autoload :Boundaries, "ohm/contrib/boundaries"
|
8
|
+
autoload :Timestamping, "ohm/contrib/timestamping"
|
9
|
+
autoload :LengthValidations, "ohm/contrib/length_validations"
|
10
|
+
autoload :WebValidations, "ohm/contrib/web_validations"
|
11
|
+
autoload :NumberValidations, "ohm/contrib/number_validations"
|
12
|
+
autoload :DateValidations, "ohm/contrib/date_validations"
|
13
|
+
autoload :ExtraValidations, "ohm/contrib/extra_validations"
|
14
|
+
autoload :Typecast, "ohm/contrib/typecast"
|
15
|
+
autoload :Locking, "ohm/contrib/locking"
|
16
|
+
autoload :Callbacks, "ohm/contrib/callbacks"
|
17
|
+
autoload :LunarMacros, "ohm/contrib/lunar_macros"
|
18
|
+
autoload :Slug, "ohm/contrib/slug"
|
19
|
+
autoload :Scope, "ohm/contrib/scope"
|
20
|
+
autoload :SoftDelete, "ohm/contrib/soft_delete"
|
21
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require "active_model"
|
4
|
+
|
5
|
+
# Extension for ActiveModel compatibility
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
#
|
9
|
+
# class Post < Ohm::Model
|
10
|
+
# include Ohm::ActiveModelExtension
|
11
|
+
# end
|
12
|
+
module Ohm
|
13
|
+
module ActiveModelExtension
|
14
|
+
def to_model
|
15
|
+
ActiveModelInterface.new(self)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class ActiveModelInterface
|
20
|
+
def initialize(model)
|
21
|
+
@model = model
|
22
|
+
end
|
23
|
+
|
24
|
+
extend ActiveModel::Naming
|
25
|
+
|
26
|
+
def to_model
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid?
|
31
|
+
@model.valid?
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_record?
|
35
|
+
@model.new?
|
36
|
+
end
|
37
|
+
|
38
|
+
def destroyed?
|
39
|
+
false
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_key
|
43
|
+
[@model.id] if persisted?
|
44
|
+
end
|
45
|
+
|
46
|
+
def persisted?
|
47
|
+
! new_record?
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_param
|
51
|
+
if persisted?
|
52
|
+
@model.respond_to?(:to_param) ?
|
53
|
+
@model.to_param :
|
54
|
+
@model.id
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def errors
|
59
|
+
Errors.new(@model.class.to_reference, @model.errors)
|
60
|
+
end
|
61
|
+
|
62
|
+
class Errors
|
63
|
+
def initialize(scope, errors)
|
64
|
+
@scope = scope
|
65
|
+
@errors = Hash.new { |hash, key| hash[key] = [] }
|
66
|
+
|
67
|
+
errors.each do |key, value|
|
68
|
+
@errors[key] << value
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def [](key)
|
73
|
+
@errors[key]
|
74
|
+
end
|
75
|
+
|
76
|
+
def full_messages
|
77
|
+
@errors.map do |key, value|
|
78
|
+
I18n::t("ohm.%s.%s.%s" % [@scope, key, value])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require File.expand_path("./helper", File.dirname(__FILE__))
|
4
|
+
|
5
|
+
require "test/unit"
|
6
|
+
require "active_model"
|
7
|
+
|
8
|
+
class ActiveModelTest < Test::Unit::TestCase
|
9
|
+
include ActiveModel::Lint::Tests
|
10
|
+
|
11
|
+
class Post < Ohm::Model
|
12
|
+
include Ohm::ActiveModelExtension
|
13
|
+
|
14
|
+
attribute :body
|
15
|
+
list :related, Post
|
16
|
+
|
17
|
+
def validate
|
18
|
+
assert_present :body
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def setup
|
23
|
+
@model = Post.new
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 40
|
9
|
+
version: 0.0.40
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Cyril David
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10
|
17
|
+
date: 2010-11-10 00:00:00 +08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -91,6 +91,7 @@ extensions: []
|
|
91
91
|
extra_rdoc_files: []
|
92
92
|
|
93
93
|
files:
|
94
|
+
- lib/ohm/contrib/active_model_extension.rb
|
94
95
|
- lib/ohm/contrib/boundaries.rb
|
95
96
|
- lib/ohm/contrib/callbacks.rb
|
96
97
|
- lib/ohm/contrib/date_validations.rb
|
@@ -109,6 +110,7 @@ files:
|
|
109
110
|
- README.markdown
|
110
111
|
- LICENSE
|
111
112
|
- Rakefile
|
113
|
+
- test/activemodel_test.rb
|
112
114
|
- test/autoload_test.rb
|
113
115
|
- test/boundaries_test.rb
|
114
116
|
- test/callbacks_lint.rb
|