syrup_form_object 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.rspec +2 -0
- data/Gemfile +9 -0
- data/README.md +42 -0
- data/lib/syrup.rb +4 -0
- data/lib/syrup/form_object.rb +26 -0
- data/lib/syrup/version.rb +3 -0
- data/spec/lib/syrup/form_object.rb +55 -0
- data/spec/spec_helper.rb +5 -0
- data/syrum_form_object.gemspec +20 -0
- metadata +11 -1
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
==Syrup::FormObject
|
2
|
+
|
3
|
+
This is a simple implementation of the FormObject... pattern?
|
4
|
+
|
5
|
+
==Installation
|
6
|
+
|
7
|
+
gem install syrup_form_object
|
8
|
+
|
9
|
+
or in the *Gemfile*
|
10
|
+
|
11
|
+
gem 'syrup_form_object'
|
12
|
+
|
13
|
+
==Examples
|
14
|
+
|
15
|
+
class EventCreateForm < Syrup::FormObject
|
16
|
+
attr_accessor :event
|
17
|
+
accepts_nested_attributes_for :event
|
18
|
+
|
19
|
+
attribute :length_of_the_event, Integer
|
20
|
+
validates :length_of_the_event, numericality:{greater_than: 0}
|
21
|
+
|
22
|
+
def save(params)
|
23
|
+
if self.valid?
|
24
|
+
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)
|
27
|
+
event.save
|
28
|
+
else
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
==Some sources for Form Objects
|
35
|
+
|
36
|
+
https://github.com/apotonick/reform An implementation of Form Objects
|
37
|
+
|
38
|
+
http://railscasts.com/episodes/416-form-objects
|
39
|
+
|
40
|
+
http://pivotallabs.com/form-backing-objects-for-fun-and-profit/
|
41
|
+
|
42
|
+
http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
|
data/lib/syrup.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
|
3
|
+
class Syrup::FormObject
|
4
|
+
include Virtus
|
5
|
+
|
6
|
+
def self.accepts_nested_attributes_for(*relations)
|
7
|
+
relations.each do |relation|
|
8
|
+
module_eval "def #{relation}_attributes=(attributes); #{relation}.attributes=attributes; end"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(params={})
|
13
|
+
build
|
14
|
+
assign_parameters(params)
|
15
|
+
end
|
16
|
+
|
17
|
+
def assign_parameters(params)
|
18
|
+
@params = params
|
19
|
+
params.each do |key, value|
|
20
|
+
self.send "#{key}=", value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def build; end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestItem
|
4
|
+
include Virtus
|
5
|
+
attribute :test_item_value, Integer
|
6
|
+
end
|
7
|
+
|
8
|
+
class TestSubclass < Syrup::FormObject
|
9
|
+
attribute :test_value, String
|
10
|
+
attr_accessor :test_item
|
11
|
+
accepts_nested_attributes_for :test_item
|
12
|
+
|
13
|
+
def build
|
14
|
+
@built = true
|
15
|
+
self.test_item = TestItem.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def has_called_build?
|
19
|
+
@built
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
describe Syrup::FormObject do
|
25
|
+
subject { TestSubclass.new(params) }
|
26
|
+
let(:params) { {} }
|
27
|
+
context '#new' do
|
28
|
+
context 'with no attributes' do
|
29
|
+
it 'creates a new object' do
|
30
|
+
expect(subject).to be_a(Syrup::FormObject)
|
31
|
+
end
|
32
|
+
it 'calls the build method' do
|
33
|
+
expect(subject).to have_called_build
|
34
|
+
end
|
35
|
+
end
|
36
|
+
context 'with a params hash' do
|
37
|
+
let(:params) {{ test_value: 'a Test',
|
38
|
+
test_item_attributes: {
|
39
|
+
test_item_value: '2'}}}
|
40
|
+
it 'creates a new object' do
|
41
|
+
expect(subject).to be_a(Syrup::FormObject)
|
42
|
+
end
|
43
|
+
it 'assigns the parameters in the right places' do
|
44
|
+
expect(subject.test_value).to eq 'a Test'
|
45
|
+
end
|
46
|
+
it 'assigns the parameters in the nested attributes' do
|
47
|
+
expect(subject.test_item.test_item_value).to eq 2
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
pending '#save'
|
53
|
+
pending '::find'
|
54
|
+
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path('../lib/syrup/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'syrup_form_object'
|
5
|
+
gem.version = Syrup::VERSION
|
6
|
+
gem.summary = "Form Objects for ActiveRecord"
|
7
|
+
gem.description = gem.summary
|
8
|
+
gem.authors = [ "Alex Siri" ]
|
9
|
+
gem.email = [ 'alexsiri7@gmail.com' ]
|
10
|
+
gem.homepage = 'https://github.com/alexsiri7/syrup_form_object'
|
11
|
+
gem.license = 'MIT'
|
12
|
+
|
13
|
+
|
14
|
+
gem.require_paths = [ "lib" ]
|
15
|
+
gem.files = `git ls-files`.split("\n")
|
16
|
+
gem.test_files = `git ls-files -- {spec}/*`.split("\n")
|
17
|
+
gem.extra_rdoc_files = %w[LICENSE README.md]
|
18
|
+
|
19
|
+
gem.add_dependency('virtus', '~> 1.0.0')
|
20
|
+
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.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -36,8 +36,17 @@ extra_rdoc_files:
|
|
36
36
|
- LICENSE
|
37
37
|
- README.md
|
38
38
|
files:
|
39
|
+
- .gitignore
|
40
|
+
- .rspec
|
41
|
+
- Gemfile
|
39
42
|
- LICENSE
|
40
43
|
- README.md
|
44
|
+
- lib/syrup.rb
|
45
|
+
- lib/syrup/form_object.rb
|
46
|
+
- lib/syrup/version.rb
|
47
|
+
- spec/lib/syrup/form_object.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
- syrum_form_object.gemspec
|
41
50
|
homepage: https://github.com/alexsiri7/syrup_form_object
|
42
51
|
licenses:
|
43
52
|
- MIT
|
@@ -64,3 +73,4 @@ signing_key:
|
|
64
73
|
specification_version: 3
|
65
74
|
summary: Form Objects for ActiveRecord
|
66
75
|
test_files: []
|
76
|
+
has_rdoc:
|