simple_model 0.2.1 → 0.2.2

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/Gemfile.lock CHANGED
@@ -1,29 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_model (0.2.0)
5
- activemodel (= 3.0.5)
6
- activesupport (= 3.0.5)
4
+ simple_model (0.2.2)
5
+ activemodel (~> 3.0.5)
6
+ activesupport (~> 3.0.5)
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
- activemodel (3.0.5)
12
- activesupport (= 3.0.5)
11
+ activemodel (3.0.8)
12
+ activesupport (= 3.0.8)
13
13
  builder (~> 2.1.2)
14
- i18n (~> 0.4)
15
- activesupport (3.0.5)
14
+ i18n (~> 0.5.0)
15
+ activesupport (3.0.8)
16
16
  builder (2.1.2)
17
17
  diff-lcs (1.1.2)
18
18
  i18n (0.5.0)
19
- rspec (2.5.0)
20
- rspec-core (~> 2.5.0)
21
- rspec-expectations (~> 2.5.0)
22
- rspec-mocks (~> 2.5.0)
23
- rspec-core (2.5.1)
24
- rspec-expectations (2.5.0)
19
+ rspec (2.6.0)
20
+ rspec-core (~> 2.6.0)
21
+ rspec-expectations (~> 2.6.0)
22
+ rspec-mocks (~> 2.6.0)
23
+ rspec-core (2.6.4)
24
+ rspec-expectations (2.6.0)
25
25
  diff-lcs (~> 1.1.2)
26
- rspec-mocks (2.5.0)
26
+ rspec-mocks (2.6.0)
27
27
 
28
28
  PLATFORMS
29
29
  ruby
data/README.md CHANGED
@@ -20,11 +20,7 @@ SimpleModel is available through [Rubygems](http://rubygems.org/gems/simple_mode
20
20
  has_booleans :paid
21
21
  has_currency :price, :default => 10.0
22
22
  has_dates :created_at
23
-
24
- private
25
- def validate
26
- validates_inclusion_of :price, :in => 10..25
27
- end
23
+ validates_inclusion_of :price, :in => 10..25
28
24
  end
29
25
 
30
26
  item = Item.new(:created_at => "12/30/2010")
@@ -34,45 +30,9 @@ SimpleModel is available through [Rubygems](http://rubygems.org/gems/simple_mode
34
30
  item.paid? # => false
35
31
  item.price # => 10.0
36
32
  item.price = '$1,024.00'
37
- item.price # => 1024.0
33
+ item.price # => #<BigDecimal:100c989d8,'0.1024E4',9(27)>
38
34
  item.valid? # => false
39
35
 
40
- ### Validation and Errors only
41
-
42
- require 'simple_model'
43
-
44
- class Item
45
- include SimpleModel::Validation
46
- include SimpleModel::Errors
47
-
48
- attr_accessor :foo
49
-
50
- private
51
- def validate
52
- validates_presence_of :foo
53
- end
54
- end
55
-
56
- item = Item.new
57
- item.valid? # => false
58
-
59
- ### Validation Only - helpful if your class already has an errors object, but lacks built in validation
60
-
61
- require 'simple_model'
62
-
63
- class Item < SimpleRecord::Base
64
- include SimpleModel::Validation
65
-
66
- attr_accessor :foo
67
-
68
- private
69
- def validate
70
- validates_presence_of :foo
71
- end
72
- end
73
-
74
- item = Item.new
75
- item.valid? # => false
76
36
 
77
37
  ## Contributing to simple_model
78
38
 
@@ -2,27 +2,61 @@ module SimpleModel
2
2
  module ErrorHelpers
3
3
 
4
4
  def errors?
5
- !self.errors.nil? && !self.errors.empty?
5
+ !self.errors.blank?
6
6
  end
7
7
 
8
+
8
9
  def errors_for_flash(options={})
9
- options[:failed_action] ||= "saving"
10
- options[:id] ||= 'errorExplanation'
11
- options[:classes] ||= ''
12
- error_string = "<div id='#{options[:id]}' class='#{options[:classes]}'><h2>#{self.errors.count}"
13
- if self.errors.length > 1
14
- error_string << " errors"
15
- else
16
- error_string << " error"
10
+ #set defaults and overwrite
11
+ options = {
12
+ :failed_action => "saving",
13
+ :id => 'errorExplanation',
14
+ :classes => ''}.merge!(options)
15
+
16
+ error_list = ""
17
+
18
+ # The active_model errors object is not a normal hash and the each method
19
+ # for the active_mode errors object does not perform as expected
20
+ # so make it a plain hash
21
+ {}.merge!(self.errors).each do |error,message|
22
+ error_list << create_error_list(error,message)
17
23
  end
24
+ error_string = "<div id='#{options[:id]}' class='#{options[:classes]}'><h2>#{self.errors_count}"
25
+ error_string << " #{puralize_errors_string(self.errors)}"
18
26
  error_string << " prevented #{options[:failed_action]}.</h2><ul>"
19
-
20
- self.errors.full_messages.each do |m|
21
- error_string << "<li>#{m}</li>"
22
- end
27
+ error_string << error_list
23
28
  error_string << "</ul></div>"
24
29
  error_string
25
30
  end
31
+
32
+ # Allow for nested errors like one might see in nested assets when using
33
+ # accepts_nested_attributes and nested forms
34
+ def create_error_list(key,value)
35
+ error_items = ""
36
+ if value.is_a?(Array)
37
+ error_items << "<li><ul>#{key.to_s.titleize} #{puralize_errors_string(value)}:"
38
+ value.each do |item|
39
+ new_value = item.to_a[0]
40
+ error_items << create_error_list(new_value[0],new_value[1])
41
+ end
42
+ error_items << "</ul></li>"
43
+ elsif value.is_a?(Hash)
44
+ error_items << "<li><ul>#{key.to_s.titleize} error:"
45
+ error_items << create_error_list(value[0],value[1])
46
+ error_items << "</ul></li>"
47
+ else
48
+ self.errors_count = (self.errors_count.to_i + 1)
49
+ error_items << "<li>#{key.to_s.titleize} #{value}</li>"
50
+ end
51
+ error_items
52
+ end
53
+
54
+ def puralize_errors_string(array)
55
+ s = "error"
56
+ s << "s" if array.length > 1
57
+ s
58
+ end
59
+
26
60
 
27
61
  def errors_to_s
28
62
  error_string = ""
@@ -1,3 +1,3 @@
1
1
  module SimpleModel
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/simple_model.gemspec CHANGED
@@ -11,10 +11,10 @@ Gem::Specification.new do |s|
11
11
  s.email = ["joshmckin@gmail.com"]
12
12
  s.homepage = ""
13
13
  s.summary = %q{Simpifies building tableless models or models backed by webservices}
14
- s.description = %q{Simpifies building tableless models or models backed by webservices. Create data type specific attributes with default if values. Also, provides a simple error and validation api for non-rails 3 apps.}
14
+ s.description = %q{Simpifies building tableless models or models backed by webservices. Create data type specific attributes with default if values.}
15
15
 
16
- s.add_runtime_dependency 'activesupport','3.0.5'
17
- s.add_runtime_dependency 'activemodel',"3.0.5"
16
+ s.add_runtime_dependency 'activesupport','~> 3.0.5'
17
+ s.add_runtime_dependency 'activemodel',"~> 3.0.5"
18
18
 
19
19
  s.add_development_dependency 'rspec'
20
20
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: simple_model
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Joshua T Mckinney
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-03 00:00:00 -05:00
13
+ date: 2011-06-08 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
20
  none: false
21
21
  requirements:
22
- - - "="
22
+ - - ~>
23
23
  - !ruby/object:Gem::Version
24
24
  version: 3.0.5
25
25
  type: :runtime
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirement: &id002 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
- - - "="
33
+ - - ~>
34
34
  - !ruby/object:Gem::Version
35
35
  version: 3.0.5
36
36
  type: :runtime
@@ -46,7 +46,7 @@ dependencies:
46
46
  version: "0"
47
47
  type: :development
48
48
  version_requirements: *id003
49
- description: Simpifies building tableless models or models backed by webservices. Create data type specific attributes with default if values. Also, provides a simple error and validation api for non-rails 3 apps.
49
+ description: Simpifies building tableless models or models backed by webservices. Create data type specific attributes with default if values.
50
50
  email:
51
51
  - joshmckin@gmail.com
52
52
  executables: []
@@ -101,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  requirements: []
102
102
 
103
103
  rubyforge_project: simple_model
104
- rubygems_version: 1.5.0
104
+ rubygems_version: 1.6.2
105
105
  signing_key:
106
106
  specification_version: 3
107
107
  summary: Simpifies building tableless models or models backed by webservices