syrup_form_object 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,18 +12,26 @@ or in the *Gemfile*
12
12
 
13
13
  ##Examples
14
14
 
15
+ To update the ```Event``` class in your model
16
+
17
+ class Event < ActiveRecord::Base
18
+ validates :start_date, presence: true
19
+ validates :end_date, presence: true
20
+ end
21
+
22
+ You create the follwing form
23
+
15
24
  class EventCreateForm < Syrup::FormObject
16
- attr_accessor :event
25
+ has_one :event
17
26
  accepts_nested_attributes_for :event
18
27
 
19
28
  attribute :length_of_the_event, Integer
20
29
  validates :length_of_the_event, numericality:{greater_than: 0}
21
30
 
22
- def save(params)
31
+ def save
23
32
  if self.valid?
24
33
  end_date = event.start_date + length_of_the_event.hours
25
- event_attributes = params[:event_attributes].merge(end_date: end_date)
26
- event = Event.new(event_attributes)
34
+ event.end_date = end_date
27
35
  event.save
28
36
  else
29
37
  false
@@ -31,6 +39,40 @@ or in the *Gemfile*
31
39
  end
32
40
  end
33
41
 
42
+ Create a controller similar to this one
43
+
44
+ class EventController < ApplicationController
45
+ def new
46
+ @event_form = EventCreateForm.new
47
+ end
48
+
49
+ def create
50
+ @event_form = EventCreateForm.new(create_params)
51
+ if @event_form.save
52
+ redirect_to @event_form.event
53
+ else
54
+ render :new
55
+ end
56
+ end
57
+
58
+ def create_params
59
+ params.require(:event_create_form)
60
+ .permit(:length_of_the_event)
61
+ .permit(event_attributes: [:start_date])
62
+ end
63
+ end
64
+
65
+
66
+ And in the template:
67
+
68
+ <%= form_for @event_form do %>
69
+ <%= fields_for :event do %>
70
+ <%= input_tag :start_date %>
71
+ <% end %>
72
+ <%= input_tag :length_of_the_event %>
73
+ <% end %>
74
+
75
+
34
76
  ##Some sources for Form Objects
35
77
 
36
78
  https://github.com/apotonick/reform An implementation of Form Objects
@@ -27,13 +27,31 @@ class Syrup::FormObject
27
27
  end
28
28
  EOH
29
29
  end
30
+
31
+ def relations
32
+ @relations ||= []
33
+ end
34
+
35
+ def has_one(klass)
36
+ relations << klass
37
+ attr_accessor klass
38
+ end
39
+
40
+
30
41
  end
31
42
 
32
43
  def initialize(params={})
44
+ build_relations
33
45
  build
34
46
  assign_parameters(params)
35
47
  end
36
48
 
49
+ def build_relations
50
+ self.class.relations.each do |klass|
51
+ self.send "#{klass}=", klass.to_s.camelize.constantize.new
52
+ end
53
+ end
54
+
37
55
  def assign_parameters(params)
38
56
  @params = params
39
57
  params.each do |key, value|
data/lib/syrup/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Syrup
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -7,12 +7,11 @@ end
7
7
 
8
8
  class TestSubclass < Syrup::FormObject
9
9
  attribute :test_value, String
10
- attr_accessor :test_item
10
+ has_one :test_item
11
11
  accepts_nested_attributes_for :test_item
12
12
 
13
13
  def build
14
14
  @built = true
15
- self.test_item = TestItem.new
16
15
  end
17
16
 
18
17
  def has_called_build?
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,16 @@
1
1
  require 'rspec'
2
2
 
3
+ class String
4
+ def constantize
5
+ Object.const_get(self)
6
+ end
7
+
8
+ def camelize
9
+ string = sub(/^[a-z\d]*/) { $&.capitalize }
10
+ string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }
11
+ end
12
+ end
13
+
3
14
  module ActiveModel
4
15
  module Naming; end
5
16
  module Conversion; end
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: