syrup_form_object 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -20
- data/lib/syrup/form_object.rb +8 -0
- data/lib/syrup/relations.rb +6 -1
- data/lib/syrup/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -21,6 +21,8 @@ gem 'syrup_form_object'
|
|
21
21
|
|
22
22
|
##Examples
|
23
23
|
|
24
|
+
Note: The following example can be found in [syrup_form_example](https://github.com/alexsiri7/syrup_form_example)
|
25
|
+
|
24
26
|
To update the ```Event``` class in your model
|
25
27
|
``` ruby
|
26
28
|
class Event < ActiveRecord::Base
|
@@ -32,21 +34,16 @@ end
|
|
32
34
|
You create the follwing form
|
33
35
|
|
34
36
|
``` ruby
|
35
|
-
class
|
36
|
-
|
37
|
-
accepts_nested_attributes_for :event
|
37
|
+
class EventForm < Syrup::FormObject
|
38
|
+
wraps :event
|
38
39
|
|
39
40
|
attribute :length_of_the_event, Integer
|
40
|
-
validates :length_of_the_event, numericality:{greater_than: 0}
|
41
|
+
validates :length_of_the_event, numericality: {greater_than: 0}
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
event.save
|
47
|
-
else
|
48
|
-
false
|
49
|
-
end
|
43
|
+
before_validation :before_validation
|
44
|
+
|
45
|
+
def before_validation
|
46
|
+
self.end_date = event.start_date + length_of_the_event.to_i.hours
|
50
47
|
end
|
51
48
|
end
|
52
49
|
```
|
@@ -56,11 +53,11 @@ Create a controller similar to this one
|
|
56
53
|
``` ruby
|
57
54
|
class EventController < ApplicationController
|
58
55
|
def new
|
59
|
-
@event_form =
|
56
|
+
@event_form = EventForm.new
|
60
57
|
end
|
61
58
|
|
62
59
|
def create
|
63
|
-
@event_form =
|
60
|
+
@event_form = EventForm.new(create_params)
|
64
61
|
if @event_form.save
|
65
62
|
redirect_to @event_form.event
|
66
63
|
else
|
@@ -69,9 +66,8 @@ class EventController < ApplicationController
|
|
69
66
|
end
|
70
67
|
|
71
68
|
def create_params
|
72
|
-
params.require(:
|
73
|
-
.permit(:length_of_the_event)
|
74
|
-
.permit(event_attributes: [:start_date])
|
69
|
+
params.require(:event)
|
70
|
+
.permit(:length_of_the_event, :start_date)
|
75
71
|
end
|
76
72
|
end
|
77
73
|
```
|
@@ -80,9 +76,7 @@ And in the template:
|
|
80
76
|
|
81
77
|
``` erb
|
82
78
|
<%= form_for @event_form do %>
|
83
|
-
<%=
|
84
|
-
<%= input_tag :start_date %>
|
85
|
-
<% end %>
|
79
|
+
<%= input_tag :start_date %>
|
86
80
|
<%= input_tag :length_of_the_event %>
|
87
81
|
<% end %>
|
88
82
|
```
|
data/lib/syrup/form_object.rb
CHANGED
@@ -23,6 +23,14 @@ class Syrup::FormObject
|
|
23
23
|
form.after_find(params)
|
24
24
|
form
|
25
25
|
end
|
26
|
+
|
27
|
+
def form_name(form_name)
|
28
|
+
instance_eval <<-EOH
|
29
|
+
def self.model_name
|
30
|
+
ActiveModel::Name.new(self, nil, '#{form_name.to_s}')
|
31
|
+
end
|
32
|
+
EOH
|
33
|
+
end
|
26
34
|
end
|
27
35
|
|
28
36
|
def remember_transaction_record_state(*); end
|
data/lib/syrup/relations.rb
CHANGED
@@ -11,6 +11,7 @@ module Syrup::Relations
|
|
11
11
|
def accepts_nested_attributes_for(*relations)
|
12
12
|
relations.each do |relation|
|
13
13
|
build_attributes_setter relation
|
14
|
+
add_relation(relation)
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
@@ -32,8 +33,12 @@ module Syrup::Relations
|
|
32
33
|
@relations ||= []
|
33
34
|
end
|
34
35
|
|
36
|
+
def add_relation(klass)
|
37
|
+
relations << klass unless relations.include?(klass)
|
38
|
+
end
|
39
|
+
|
35
40
|
def has_one(klass)
|
36
|
-
|
41
|
+
add_relation(klass)
|
37
42
|
attr_accessor klass
|
38
43
|
end
|
39
44
|
end
|
data/lib/syrup/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syrup_form_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
12
|
+
date: 2013-11-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: virtus
|