json_record 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGE_LOG ADDED
@@ -0,0 +1,13 @@
1
+ 1.0.0
2
+ - Initial release
3
+
4
+ 1.0.1
5
+ - Fixed bug where embedded documents couldn't reference the schema until a field was defined
6
+
7
+ 1.0.2
8
+ - Changed EmbeddedDocument to be a module instead of a class to fix inheritance problem with validations
9
+ - Remove key and many methods from EmbeddedDocument in favor of always calling the schema
10
+
11
+ 1.0.3
12
+ - Added before and after validation callbacks on EmbeddedDocument
13
+ - Fixed bug where fields couldn't be set to false
data/README.rdoc CHANGED
@@ -21,6 +21,8 @@ To make you flexible schema really powerful, add some embedded documents to it.
21
21
 
22
22
  Embedded documents have their own schema that is serialized to JSON. This schema can also contain embedded documents allowing you to easily create very rich data structures all with only one database table. And because there is only one table, you don't need to worry at all about ensuring your changes to embedded documents are saved along with the parent record.
23
23
 
24
+ Embedded documents support validations and before_validation and after_validation callbacks.
25
+
24
26
  == Example
25
27
 
26
28
  class Post < ActiveRecord::Base
@@ -43,6 +45,10 @@ Embedded documents have their own schema that is serialized to JSON. This schema
43
45
  schema.key :author, Person, :required => true
44
46
  schema.key :body, :required => true
45
47
  schema.many :replies, Comment
48
+
49
+ after_validation do |comment|
50
+ comment.body = ERB::Util.html_escape(comment.body)
51
+ end
46
52
  end
47
53
 
48
54
  Create a new post with a title and author:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.3
data/json_record.gemspec CHANGED
@@ -5,17 +5,18 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{json_record}
8
- s.version = "1.0.2"
8
+ s.version = "1.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brian Durand"]
12
- s.date = %q{2010-02-16}
12
+ s.date = %q{2010-02-20}
13
13
  s.email = %q{brian@embellishedvisions.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.rdoc"
16
16
  ]
17
17
  s.files = [
18
- "MIT_LICENSE",
18
+ "CHANGE_LOG",
19
+ "MIT_LICENSE",
19
20
  "README.rdoc",
20
21
  "Rakefile",
21
22
  "VERSION",
@@ -37,6 +37,16 @@ module JsonRecord
37
37
  def new_record?; false; end;
38
38
  end
39
39
 
40
+ module ActiveSupport3Callbacks #:nodoc:
41
+ def before_validation (*args, &block)
42
+ set_callback(:validation, :before, *args, &block)
43
+ end
44
+
45
+ def after_validation (*args, &block)
46
+ set_callback(:validation, :after, *args, &block)
47
+ end
48
+ end
49
+
40
50
  # Classes that include EmbeddedDocument can be used as the type for keys or many field definitions
41
51
  # in Schema. Embedded documents are then extensions of the schema. In this way, complex
42
52
  # documents represented in JSON can be deserialized as complex objects.
@@ -47,7 +57,20 @@ module JsonRecord
47
57
  base.send :include, ActiveRecordStub
48
58
  base.send :include, ActiveRecord::Validations
49
59
  base.send :include, AttributeMethods
50
-
60
+ base.send :include, ActiveSupport::Callbacks
61
+
62
+ if base.respond_to?(:set_callback)
63
+ # Poor man's check for ActiveSupport 3.0 which completely changed around how callbacks work.
64
+ # This is a temporary work around so that the same gem can be compatible with both 2.x and 3.x for now.
65
+ # Incoporating ActiveModel will fix all.
66
+ base.define_callbacks :validation
67
+ base.alias_method_chain(:valid?, :callbacks_3)
68
+ base.extend(ActiveSupport3Callbacks)
69
+ else
70
+ base.define_callbacks :before_validation, :after_validation
71
+ base.alias_method_chain(:valid?, :callbacks)
72
+ end
73
+
51
74
  base.write_inheritable_attribute(:schema, Schema.new(base, nil))
52
75
  base.class_inheritable_reader :schema
53
76
  end
@@ -110,6 +133,19 @@ module JsonRecord
110
133
  "#<#{self.class.name} #{attributes.inspect}>"
111
134
  end
112
135
 
136
+ def valid_with_callbacks? #:nodoc:
137
+ run_callbacks(:before_validation)
138
+ valid = valid_without_callbacks?
139
+ run_callbacks(:after_validation)
140
+ valid
141
+ end
142
+
143
+ def valid_with_callbacks_3? #:nodoc:
144
+ run_callbacks(:validation) do
145
+ valid_without_callbacks_3?
146
+ end
147
+ end
148
+
113
149
  protected
114
150
 
115
151
  def json_attributes
@@ -34,7 +34,7 @@ module JsonRecord
34
34
  # the original value will be returned. Blank values are always translated to nil. Hashes will be converted
35
35
  # to EmbeddedDocument objects if the field type extends from EmbeddedDocument.
36
36
  def convert (val)
37
- return nil if val.blank?
37
+ return nil if val.blank? and val != false
38
38
  if @type == String
39
39
  return val.to_s
40
40
  elsif @type == Integer
@@ -428,4 +428,9 @@ describe JsonRecord::Serialized do
428
428
  subtrait.errors[:count].should be_blank
429
429
  end
430
430
 
431
+ it "should perform validation callbacks on embedded documents" do
432
+ trait = JsonRecord::Test::Trait.new(:name => "name")
433
+ trait.valid?.should == true
434
+ trait.callbacks.should == [:before_validation, :after_validation]
435
+ end
431
436
  end
data/spec/test_models.rb CHANGED
@@ -33,6 +33,10 @@ module JsonRecord
33
33
  schema.key :value
34
34
  schema.key :count, Integer
35
35
  schema.many :sub_traits, Trait, :unique => [:name, :value]
36
+
37
+ attr_accessor :callbacks
38
+ before_validation{|record| record.callbacks ||= []; record.callbacks << :before_validation}
39
+ after_validation{|record| record.callbacks ||= []; record.callbacks << :after_validation}
36
40
  end
37
41
 
38
42
  class Dimension
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-16 00:00:00 -06:00
12
+ date: 2010-02-20 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -51,6 +51,7 @@ extensions: []
51
51
  extra_rdoc_files:
52
52
  - README.rdoc
53
53
  files:
54
+ - CHANGE_LOG
54
55
  - MIT_LICENSE
55
56
  - README.rdoc
56
57
  - Rakefile