shadow_form 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +24 -0
- data/.rspec +4 -0
- data/.rubocop.yml +48 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/Guardfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/lib/shadow_form.rb +40 -0
- data/shadow_form.gemspec +31 -0
- data/spec/lib/shadow_form_spec.rb +39 -0
- data/spec/spec_helper.rb +30 -0
- metadata +187 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 35452969057eecdbd8131f0c62bd369f124edd30
|
4
|
+
data.tar.gz: c96c2bff554025b083a1531c8e3acc84992e4357
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85597e6edbfa1a6a65b5ad042693375723c8627360b28363e2607c3ea74a7fd36d022e070f5a23cd172107dbbfd434e60f6f1a2393dfcf703144ed42a43ecaff
|
7
|
+
data.tar.gz: 44a7b4143ca9c961edb14dcb9bb04ddac588d8f390f68bb685f93aa5af648e1c49ef5b46617f6285e22e904d1d164f7a05211635003d429e4965843ae0b04f75
|
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.rubocop_todo.yml
|
24
|
+
.ruby-version
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2015-02-23 06:17:38 +0100 using RuboCop version 0.27.1.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 3
|
9
|
+
Style/Documentation:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
# Offense count: 2
|
13
|
+
# Cop supports --auto-correct.
|
14
|
+
# Configuration parameters: PreferredDelimiters.
|
15
|
+
Style/PercentLiteralDelimiters:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
# Offense count: 2
|
19
|
+
Style/RegexpLiteral:
|
20
|
+
MaxSlashes: 0
|
21
|
+
|
22
|
+
# Offense count: 1
|
23
|
+
# Cop supports --auto-correct.
|
24
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
25
|
+
Style/TrailingBlankLines:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
# Offense count: 1
|
29
|
+
# Cop supports --auto-correct.
|
30
|
+
# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, Whitelist.
|
31
|
+
Style/TrivialAccessors:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
# Offense count: 2
|
35
|
+
# Cop supports --auto-correct.
|
36
|
+
Style/UnneededPercentQ:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Documentation:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
Metrics/LineLength:
|
43
|
+
Max: 120
|
44
|
+
|
45
|
+
AllCops:
|
46
|
+
Exclude:
|
47
|
+
- '**/Guardfile'
|
48
|
+
- '**/shadow_form.gemspec'
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
guard :rspec, cmd: 'rspec' do
|
2
|
+
watch(%r{^lib/(.+).rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
3
|
+
watch(%r{^spec/(.+).rb$}) { |m| "spec/#{m[1]}.rb" }
|
4
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
5
|
+
watch('Gemfile')
|
6
|
+
end
|
7
|
+
|
8
|
+
guard :rubocop, all_on_start: false, cli: ['--format', 'clang'] do
|
9
|
+
watch(%r{.+\.rb$})
|
10
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
11
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Pawel Niemczyk
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# ShadowForm
|
2
|
+
|
3
|
+
Fat model with validation on all cases always causes problems in the future. So my solution is ShadowForm. Now you can have your original model without validation and use form when you need it.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'shadow_form'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install shadow_form
|
21
|
+
|
22
|
+
## How it works
|
23
|
+
You have your active_record or active_model model:
|
24
|
+
```ruby
|
25
|
+
class User
|
26
|
+
include ActiveModel::Model
|
27
|
+
attr_accessor :name, :email, :password
|
28
|
+
end
|
29
|
+
```
|
30
|
+
And for example register form
|
31
|
+
```ruby
|
32
|
+
class UserRegistrationForm < ShadowForm::Core
|
33
|
+
shadow_of User
|
34
|
+
give_name 'RegisterForm` # optional
|
35
|
+
validation do
|
36
|
+
validates :email, presence: true
|
37
|
+
validates :password, presence: true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
And now you have `UserRegistrationForm` like `User` + `Validation`
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it ( https://github.com/[my-github-username]shadow_form/fork )
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/shadow_form.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module ShadowForm
|
2
|
+
VERSION = '0.0.1'
|
3
|
+
class Core
|
4
|
+
class << self
|
5
|
+
def shadow_of(klass)
|
6
|
+
form_config[:shadow_of] = klass
|
7
|
+
end
|
8
|
+
|
9
|
+
def validation(&block)
|
10
|
+
form_config[:validation] = block
|
11
|
+
end
|
12
|
+
|
13
|
+
def give_name(new_name)
|
14
|
+
form_config[:new_name] = new_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def new(*args)
|
18
|
+
form_class.new(*args)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def form_class
|
24
|
+
@form_class ||= (
|
25
|
+
klass = form_config[:shadow_of] || fail('[ShadowForm] missing shadow class')
|
26
|
+
klass_name = form_config.fetch(:new_name, name)
|
27
|
+
Class.new(klass).tap do |m_class|
|
28
|
+
self.const_set('ShadowForm', m_class)
|
29
|
+
m_class.class_eval("def self.name; \"#{klass_name}\"; end")
|
30
|
+
m_class.class_eval(&form_config[:validation]) if form_config[:validation]
|
31
|
+
end
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
def form_config
|
36
|
+
@form_config ||= {}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/shadow_form.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'shadow_form'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'shadow_form'
|
9
|
+
spec.version = ShadowForm::VERSION
|
10
|
+
spec.authors = ['Pawel Niemczyk']
|
11
|
+
spec.email = ['pniemczyk.info@.gmail.com']
|
12
|
+
spec.summary = %q{This is simple wrapper on the model to prepare forms for all validation usage cases}
|
13
|
+
spec.description = %q{This is simple wrapper on the model to prepare forms for all validation usage cases}
|
14
|
+
spec.homepage = 'https://github.com/pniemczyk/shadow_form'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.4'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
spec.add_development_dependency 'guard', '~> 2.12'
|
26
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.5'
|
27
|
+
spec.add_development_dependency 'guard-rubocop', '~> 1.2'
|
28
|
+
spec.add_development_dependency 'coveralls', '~> 0.7'
|
29
|
+
spec.add_development_dependency 'activemodel', '~> 4.2'
|
30
|
+
spec.add_development_dependency 'awesome_print', '~> 1.6'
|
31
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
describe ShadowForm::Core do
|
5
|
+
|
6
|
+
class PersonModel
|
7
|
+
include ActiveModel::Model
|
8
|
+
attr_accessor :name, :age
|
9
|
+
end
|
10
|
+
|
11
|
+
def subject_class
|
12
|
+
Class.new(described_class).tap do |klass|
|
13
|
+
klass.class_eval do
|
14
|
+
shadow_of PersonModel
|
15
|
+
give_name 'PersonForm'
|
16
|
+
validation do
|
17
|
+
validates :name, presence: true
|
18
|
+
validates :age, numericality: { only_integer: true, greater_than: 17}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:attributes) { {} }
|
25
|
+
subject { subject_class.new(attributes) }
|
26
|
+
|
27
|
+
it '.name is set by self.give_name method' do
|
28
|
+
expect(subject.class.name).to eq('PersonForm')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'form class ancestors include PersonModel' do
|
32
|
+
expect(subject.class.ancestors).to include(PersonModel)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'validation is present' do
|
36
|
+
expect(subject.valid?).to eq(false)
|
37
|
+
expect(subject.errors.as_json).to eq(name: ["can't be blank"], age: ['is not a number'])
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'awesome_print'
|
3
|
+
Bundler.setup
|
4
|
+
|
5
|
+
require 'coveralls'
|
6
|
+
Coveralls.wear!
|
7
|
+
|
8
|
+
require 'shadow_form'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.filter_run :focus
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
|
14
|
+
config.default_formatter = 'doc' if config.files_to_run.one?
|
15
|
+
|
16
|
+
config.profile_examples = 10
|
17
|
+
config.order = :random
|
18
|
+
|
19
|
+
Kernel.srand config.seed
|
20
|
+
|
21
|
+
config.expect_with :rspec do |expectations|
|
22
|
+
expectations.syntax = :expect
|
23
|
+
end
|
24
|
+
|
25
|
+
config.mock_with :rspec do |mocks|
|
26
|
+
mocks.syntax = :expect
|
27
|
+
mocks.verify_partial_doubles = true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
require 'guard/rubocop'
|
metadata
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shadow_form
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pawel Niemczyk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.4'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.2'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: coveralls
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.7'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.7'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activemodel
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '4.2'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '4.2'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: awesome_print
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '1.6'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '1.6'
|
139
|
+
description: This is simple wrapper on the model to prepare forms for all validation
|
140
|
+
usage cases
|
141
|
+
email:
|
142
|
+
- pniemczyk.info@.gmail.com
|
143
|
+
executables: []
|
144
|
+
extensions: []
|
145
|
+
extra_rdoc_files: []
|
146
|
+
files:
|
147
|
+
- ".gitignore"
|
148
|
+
- ".rspec"
|
149
|
+
- ".rubocop.yml"
|
150
|
+
- ".travis.yml"
|
151
|
+
- Gemfile
|
152
|
+
- Guardfile
|
153
|
+
- LICENSE.txt
|
154
|
+
- README.md
|
155
|
+
- Rakefile
|
156
|
+
- lib/shadow_form.rb
|
157
|
+
- shadow_form.gemspec
|
158
|
+
- spec/lib/shadow_form_spec.rb
|
159
|
+
- spec/spec_helper.rb
|
160
|
+
homepage: https://github.com/pniemczyk/shadow_form
|
161
|
+
licenses:
|
162
|
+
- MIT
|
163
|
+
metadata: {}
|
164
|
+
post_install_message:
|
165
|
+
rdoc_options: []
|
166
|
+
require_paths:
|
167
|
+
- lib
|
168
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
requirements: []
|
179
|
+
rubyforge_project:
|
180
|
+
rubygems_version: 2.4.3
|
181
|
+
signing_key:
|
182
|
+
specification_version: 4
|
183
|
+
summary: This is simple wrapper on the model to prepare forms for all validation usage
|
184
|
+
cases
|
185
|
+
test_files:
|
186
|
+
- spec/lib/shadow_form_spec.rb
|
187
|
+
- spec/spec_helper.rb
|