gourami 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6350a0c25f3ae177cd9f7f1679421265ca935c28
4
- data.tar.gz: 67acb125ddd4c6641bf804cc3ceb1f7330b9f09a
3
+ metadata.gz: f7d45d0bcf1edbc6a81d3489bab6de5ccd4b0ea2
4
+ data.tar.gz: d04dff73bc07b0ad96aa619f11f710b6024b02e9
5
5
  SHA512:
6
- metadata.gz: 941e297d65e2e2fda1c4636c3662360293f9bd2a0e9789446e397446ff176330fe4944fc16ce69867f531dd92a395b9a6e848d485e248670c1b1f60bb1f2c5dc
7
- data.tar.gz: 913a0865ffd2357dab99423ac30ce46c737ebf74929404e245fb28c4fe92e76e1f9326d06e72767129d00b3067bb2de2cbf256787fd648e9257ce5f08c7db7a9
6
+ metadata.gz: 95bbf65d77ce12927bed88dbeab0ceacd4cd9d57654d163e94e827f18623ca39cfb029d5e67d9a56593809c9ebcf27220e273a7ddd70fd451a098c62d35b25b1
7
+ data.tar.gz: 10eacdc7e10de57f2daa4c600d331936984753ee5749445c4bbda6e867442a36e14bdb8eb509fa9bbc65f54fcc2baa78d410158ad8c40bac6288cdaff63ac5f2
data/README.md CHANGED
@@ -20,7 +20,160 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ ### A Typical Gourami::Form will
24
+
25
+ - list a set of attributes
26
+ - validate user input
27
+ - perform an action
28
+
29
+ ```ruby
30
+ class TypicalForm < Gourami::Form
31
+
32
+ attribute(:typical_attribute)
33
+
34
+ def validate
35
+ # Define Your validation rules here
36
+ end
37
+
38
+ def perform
39
+ # Perform your action rules here
40
+ end
41
+
42
+ end
43
+ ```
44
+
45
+ ### Your Rails 5 ActionController for the New/Create action:
46
+
47
+ ```ruby
48
+ def new
49
+ @form = CreateFishTank.new
50
+ end
51
+
52
+ def create
53
+ @form = CreateFishTank.new(fish_tank_params)
54
+
55
+ if @form.valid?
56
+ @form.perform
57
+ redirect_to @form.record
58
+ else
59
+ render "new"
60
+ end
61
+ end
62
+ ```
63
+
64
+ ### Example of a form that Creates a record
65
+
66
+ ```ruby
67
+ class CreateFishTank < Gourami::Form
68
+
69
+ record(:fish_tank)
70
+
71
+ attribute(:width, type: :integer)
72
+ attribute(:height, type: :integer)
73
+ attribute(:liters, type: :float)
74
+ attribute(:name, type: :string)
75
+ attribute(:filter_included, type: :boolean, default: false)
76
+
77
+ def validate
78
+ validate_presence(:width)
79
+ validate_range(:width, min: 50, max: 1000)
80
+
81
+ validate_presence(:height)
82
+ validate_range(:height, min: 50, max: 1000)
83
+
84
+ validate_presence(:liters)
85
+ validate_range(:liters, min: 5, max: 200)
86
+
87
+ validate_presence(:name)
88
+ validate_uniqueness(:name) do |name|
89
+ FishTank.where(name: name).empty?
90
+ end
91
+ end
92
+
93
+ def perform
94
+ self.fish_tank = FishTank.create(attributes)
95
+ end
96
+
97
+ end
98
+ ```
99
+
100
+ ### Your Rails 5 ActionController for the Edit/Update action:
101
+
102
+ ```ruby
103
+ def edit
104
+ fish_tank = FishTank.find(params[:id])
105
+ @form = UpdateFishTank.new_from_record(fish_tank)
106
+ end
107
+
108
+ def update
109
+ @form = UpdateFishTank.new(fish_tank_params)
110
+
111
+ if @form.valid?
112
+ @form.perform
113
+ redirect_to @form.record
114
+ else
115
+ render "edit"
116
+ end
117
+ end
118
+ ```
119
+
120
+ ### Example of a form that Updates a record
121
+
122
+ ```ruby
123
+ class UpdateFishTank < CreateFishTank
124
+
125
+ record(:fish_tank)
126
+
127
+ attribute(:width, type: :integer)
128
+ attribute(:height, type: :integer)
129
+ attribute(:liters, type: :float)
130
+ attribute(:name, type: :string)
131
+ attribute(:filter_included, type: :boolean, default: false)
132
+
133
+ def self.new_from_record(fish_tank)
134
+ new({ fish_tank: fish_tank }.merge(fish_tank.attributes))
135
+ end
136
+
137
+ def validate
138
+ validate_presence(:width)
139
+ validate_range(:width, min: 50, max: 1000)
140
+
141
+ validate_presence(:height)
142
+ validate_range(:height, min: 50, max: 1000)
143
+
144
+ validate_presence(:liters)
145
+ validate_range(:liters, min: 5, max: 200)
146
+
147
+ validate_presence(:name)
148
+ validate_uniqueness(:name) do |name|
149
+ FishTank.where(name: name).empty?
150
+ end
151
+ end
152
+
153
+ def perform
154
+ fish_tank.update(attributes)
155
+ end
156
+
157
+ end
158
+ ```
159
+
160
+ #### Or inherit instead of duplicating the attributes and validations
161
+
162
+ ```ruby
163
+ class UpdateFishTank < CreateFishTank
164
+
165
+ # All attributes and validations inherited from CreateFishTank.
166
+
167
+ def self.new_from_record(fish_tank)
168
+ new({ fish_tank: fish_tank }.merge(fish_tank.attributes))
169
+ end
170
+
171
+ def perform
172
+ fish_tank.update(attributes)
173
+ end
174
+
175
+ end
176
+ ```
24
177
 
25
178
  ## Development
26
179
 
data/gourami.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
+ spec.add_development_dependency "pry", "~>0.10"
24
25
  spec.add_development_dependency "bundler", "~> 1.13"
25
26
  spec.add_development_dependency "rake", "~> 10.0"
26
27
  spec.add_development_dependency "minitest", "~> 5.0"
data/lib/gourami.rb CHANGED
@@ -3,6 +3,7 @@ end
3
3
 
4
4
  require "gourami/attributes"
5
5
  require "gourami/coercer"
6
+ require "gourami/extensions/resources"
6
7
  require "gourami/form"
7
8
  require "gourami/formatting_constants"
8
9
  require "gourami/required_attribute_error"
@@ -5,6 +5,10 @@ module Gourami
5
5
 
6
6
  include Gourami::FormattingConstants
7
7
 
8
+ def validate
9
+ # Override to add custom validations
10
+ end
11
+
8
12
  # Validate and perform the form actions. If any errors come up during the
9
13
  # validation or the #perform method, raise an exception with the errors.
10
14
  #
@@ -40,6 +44,19 @@ module Gourami
40
44
  !any_errors?
41
45
  end
42
46
 
47
+ # Replace the existing errors with the provided errors Hash.
48
+ #
49
+ # @param new_errors [Hash<Symbol, nil>, Array<Symbol, String>]
50
+ #
51
+ # @return [Hash<Symbol, nil>, Array<Symbol, String>]
52
+ def clear_and_set_errors(new_errors)
53
+ new_errors = new_errors.dup
54
+ errors.clear
55
+ errors.merge!(new_errors)
56
+
57
+ errors
58
+ end
59
+
43
60
  # Return true if there given attribute has any errors.
44
61
  def attribute_has_errors?(attribute_name)
45
62
  errors[attribute_name.to_sym].any?
@@ -1,3 +1,3 @@
1
1
  module Gourami
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1".freeze
3
3
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gourami
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TSMMark
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-13 00:00:00.000000000 Z
11
+ date: 2017-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.10'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement