ohm-contrib 0.0.39 → 0.0.40

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,21 @@
1
1
  module Ohm
2
2
  module Contrib
3
- VERSION = "0.0.39"
3
+ VERSION = "0.0.40"
4
4
  end
5
5
 
6
- autoload :Boundaries, "ohm/contrib/boundaries"
7
- autoload :Timestamping, "ohm/contrib/timestamping"
8
- autoload :LengthValidations, "ohm/contrib/length_validations"
9
- autoload :WebValidations, "ohm/contrib/web_validations"
10
- autoload :NumberValidations, "ohm/contrib/number_validations"
11
- autoload :DateValidations, "ohm/contrib/date_validations"
12
- autoload :ExtraValidations, "ohm/contrib/extra_validations"
13
- autoload :Typecast, "ohm/contrib/typecast"
14
- autoload :Locking, "ohm/contrib/locking"
15
- autoload :Callbacks, "ohm/contrib/callbacks"
16
- autoload :LunarMacros, "ohm/contrib/lunar_macros"
17
- autoload :Slug, "ohm/contrib/slug"
18
- autoload :Scope, "ohm/contrib/scope"
19
- autoload :SoftDelete, "ohm/contrib/soft_delete"
20
- end
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
- - 39
9
- version: 0.0.39
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-27 00:00:00 +08:00
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