form_object 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +37 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +11 -0
- data/TODO +15 -0
- data/form_object.gemspec +23 -0
- data/lib/form_object.rb +9 -0
- data/lib/form_object/base.rb +16 -0
- data/lib/form_object/integrations.rb +43 -0
- data/lib/form_object/integrations/active_model.rb +12 -0
- data/lib/form_object/integrations/active_record.rb +12 -0
- data/lib/form_object/integrations/base.rb +111 -0
- data/lib/form_object/version.rb +3 -0
- data/test/lib/form_object_test.rb +30 -0
- data/test/lib/form_relation_test.rb +12 -0
- data/test/lib/forms_collection_test.rb +11 -0
- data/test/lib/integrations/base_test.rb +19 -0
- data/test/support/filter.rb +6 -0
- data/test/support/model.rb +0 -0
- data/test/test_helper.rb +10 -0
- metadata +111 -0
data/.gitignore
ADDED
@@ -0,0 +1,37 @@
|
|
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
|
+
|
19
|
+
# Vim
|
20
|
+
.*.sw[a-z]
|
21
|
+
*.un~
|
22
|
+
Session.vim
|
23
|
+
.netrwhist
|
24
|
+
|
25
|
+
# OSX
|
26
|
+
.DS_Store
|
27
|
+
.AppleDouble
|
28
|
+
.LSOverride
|
29
|
+
Icon
|
30
|
+
|
31
|
+
|
32
|
+
# Thumbnails
|
33
|
+
._*
|
34
|
+
|
35
|
+
# Files that might appear on external disk
|
36
|
+
.Spotlight-V100
|
37
|
+
.Trashes
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kirillov Alexander
|
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,32 @@
|
|
1
|
+
# FormObject
|
2
|
+
|
3
|
+
[](https://travis-ci.org/saratovsource/form_object)
|
4
|
+
[](https://codeclimate.com/github/saratovsource/form_object)
|
5
|
+
|
6
|
+
TODO: Write a gem description
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'form_object'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install form_object
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
TODO: Write usage instructions here
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/TODO
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Features list
|
2
|
+
|
3
|
+
* Features
|
4
|
+
- Model:
|
5
|
+
- Get the FormObject with filled attributes
|
6
|
+
- Forms collection for retrive named form
|
7
|
+
- FormObject:
|
8
|
+
- Get the model with filled attributes
|
9
|
+
- Create builder for markup (form_for, simple_form_for, etc...)
|
10
|
+
|
11
|
+
* Integrations
|
12
|
+
- ActiveRecord
|
13
|
+
- Mongoid
|
14
|
+
- DataMapper 1.x
|
15
|
+
- DataMapper 2.x
|
data/form_object.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'form_object/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "form_object"
|
8
|
+
gem.version = FormObject::VERSION
|
9
|
+
gem.authors = ["Kirillov Alexander"]
|
10
|
+
gem.email = ["saratovsource@gmail.com"]
|
11
|
+
gem.description = "Form objects for ruby on rails applications."
|
12
|
+
gem.summary = "Form object implementation"
|
13
|
+
|
14
|
+
gem.rubyforge_project = "form_object"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_dependency 'virtus'
|
22
|
+
gem.add_dependency 'activemodel'
|
23
|
+
end
|
data/lib/form_object.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module FormObject
|
2
|
+
class Base
|
3
|
+
include Virtus
|
4
|
+
|
5
|
+
extend ActiveModel::Naming
|
6
|
+
include ActiveModel::Conversion
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
# Forms are never themselves persisted
|
10
|
+
def persisted?; false; end
|
11
|
+
|
12
|
+
def model
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'form_object/integrations/base'
|
2
|
+
Dir["#{File.dirname(__FILE__)}/integrations/*.rb"].sort.each do |path|
|
3
|
+
require "form_object/integrations/#{File.basename(path)}"
|
4
|
+
end
|
5
|
+
|
6
|
+
module FormObject
|
7
|
+
# Invalid integration error
|
8
|
+
class IntegrationNotFound < StandardError
|
9
|
+
def initialize( name )
|
10
|
+
super "#{name.inspect} is invalid integration"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module Integrations
|
15
|
+
|
16
|
+
# Get collection of all integrations
|
17
|
+
# ActiveModel must be last always
|
18
|
+
# == Example
|
19
|
+
#
|
20
|
+
# Object::Integrations.all
|
21
|
+
# # => [FormObject::Integrations::ActiveRecord]
|
22
|
+
#
|
23
|
+
def self.all
|
24
|
+
constants = self.constants.map{ |c| c.to_s }
|
25
|
+
.select{ |c| c!= 'ActiveModel' }
|
26
|
+
.sort << 'ActiveModel'
|
27
|
+
constants.map{ |c| const_get(c) }
|
28
|
+
end
|
29
|
+
|
30
|
+
# Find integration by name
|
31
|
+
#
|
32
|
+
# == Examples
|
33
|
+
# FormObject::Integrations.find(:active_record) # => FormObject::Integrations::ActiveRecord
|
34
|
+
#
|
35
|
+
def self.find( name )
|
36
|
+
all.detect {|i| i.integration_name == name} || raise(IntegrationNotFound.new(name))
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.match( *args )
|
40
|
+
all.detect {|i| i.matches_ancestors?(args)}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module FormObject
|
2
|
+
module Integrations
|
3
|
+
module Base
|
4
|
+
module ClassMethods
|
5
|
+
# The default options to use for state machines using this integration
|
6
|
+
attr_reader :defaults
|
7
|
+
|
8
|
+
# FormsCollection for this model
|
9
|
+
#
|
10
|
+
# == Example
|
11
|
+
#
|
12
|
+
# class BaseForm < FormObject::Base
|
13
|
+
# model User
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# class TwitterForm < FormObject::Base
|
17
|
+
# model User, as: :twitter
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# @user = User.new
|
21
|
+
# @base_form = @user.forms[:base] # => instance of BaseForm
|
22
|
+
# @twitter_form = @iser.forms[:twitter] # => instance of TwitterForm
|
23
|
+
#
|
24
|
+
# Forms must have names. If the form name is not specified, it is taken
|
25
|
+
# from class name.
|
26
|
+
def forms
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
|
30
|
+
# The name of the integration
|
31
|
+
def integration_name
|
32
|
+
@integration_name ||= begin
|
33
|
+
name = self.name.split('::').last
|
34
|
+
name.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
35
|
+
name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
36
|
+
name.downcase!
|
37
|
+
name.to_sym
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Whether this integration is available for the current library. This
|
42
|
+
# is only true if the ORM that the integration is for is currently
|
43
|
+
# defined.
|
44
|
+
def available?
|
45
|
+
matching_ancestors.any? && Object.const_defined?(matching_ancestors[0].split('::')[0])
|
46
|
+
end
|
47
|
+
|
48
|
+
# The list of ancestor names that cause this integration to matched.
|
49
|
+
def matching_ancestors
|
50
|
+
[]
|
51
|
+
end
|
52
|
+
|
53
|
+
# Whether the integration should be used for the given class.
|
54
|
+
def matches?(klasses)
|
55
|
+
ancestor_names = klasses.map {|klass| klass.ancestors.map(&:name)}
|
56
|
+
matches_ancestors?(ancestor_names)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Whether the integration should be used for the given list of ancestors.
|
60
|
+
def matches_ancestors?(ancestors)
|
61
|
+
(ancestors & matching_ancestors).any?
|
62
|
+
end
|
63
|
+
|
64
|
+
# The path to the locale file containing translations for this
|
65
|
+
# integration. This file will only exist for integrations that actually
|
66
|
+
# support i18n.
|
67
|
+
def locale_path
|
68
|
+
path = "#{File.dirname(__FILE__)}/#{integration_name}/locale.rb"
|
69
|
+
path if File.exists?(path)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Extends the given object with any version overrides that are currently
|
73
|
+
# active
|
74
|
+
def extended(base)
|
75
|
+
versions.each do |version|
|
76
|
+
base.extend(version) if version.active?
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Defined versions
|
81
|
+
def versions
|
82
|
+
@versions ||= []
|
83
|
+
end
|
84
|
+
|
85
|
+
# DSL for define version and version based methods
|
86
|
+
#
|
87
|
+
def version( name, &block )
|
88
|
+
mod = Module.new(&block)
|
89
|
+
versions << mod
|
90
|
+
mod
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
extend ClassMethods
|
96
|
+
|
97
|
+
module InstanceMethods
|
98
|
+
|
99
|
+
# FormsCollection for this model
|
100
|
+
def forms
|
101
|
+
self.class.forms
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.included(receiver) #:nodoc:
|
106
|
+
receiver.extend ClassMethods
|
107
|
+
receiver.send :include, InstanceMethods
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FormObjectTest < TestCase
|
4
|
+
def setup
|
5
|
+
@form = Filter.new(query: "find me")
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_should_form_not_persisted
|
9
|
+
assert !@form.persisted?
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_form_should_have_query_attribute
|
13
|
+
assert "find me", @form.query
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_be_valid_form
|
17
|
+
assert @form.valid?
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_be_invalid_with_empty_query
|
21
|
+
@form.query = ""
|
22
|
+
assert !@form.valid?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_have_not_nil_created_at_attribute
|
26
|
+
assert @form.created_at.kind_of?(DateTime)
|
27
|
+
assert @form.created_at.present?
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Integrations
|
4
|
+
|
5
|
+
class BaseTest < ::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
superclass = Class.new
|
9
|
+
self.class.const_set('Vehicle', superclass)
|
10
|
+
|
11
|
+
@klass = Class.new(superclass)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_should_return_nil_if_match_not_found
|
15
|
+
assert_nil FormObject::Integrations.match(@klass)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
File without changes
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: form_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kirillov Alexander
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: virtus
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activemodel
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Form objects for ruby on rails applications.
|
47
|
+
email:
|
48
|
+
- saratovsource@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- TODO
|
59
|
+
- form_object.gemspec
|
60
|
+
- lib/form_object.rb
|
61
|
+
- lib/form_object/base.rb
|
62
|
+
- lib/form_object/integrations.rb
|
63
|
+
- lib/form_object/integrations/active_model.rb
|
64
|
+
- lib/form_object/integrations/active_record.rb
|
65
|
+
- lib/form_object/integrations/base.rb
|
66
|
+
- lib/form_object/version.rb
|
67
|
+
- test/lib/form_object_test.rb
|
68
|
+
- test/lib/form_relation_test.rb
|
69
|
+
- test/lib/forms_collection_test.rb
|
70
|
+
- test/lib/integrations/base_test.rb
|
71
|
+
- test/support/filter.rb
|
72
|
+
- test/support/model.rb
|
73
|
+
- test/test_helper.rb
|
74
|
+
homepage:
|
75
|
+
licenses: []
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
hash: -476988463
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
hash: -476988463
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project: form_object
|
100
|
+
rubygems_version: 1.8.24
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Form object implementation
|
104
|
+
test_files:
|
105
|
+
- test/lib/form_object_test.rb
|
106
|
+
- test/lib/form_relation_test.rb
|
107
|
+
- test/lib/forms_collection_test.rb
|
108
|
+
- test/lib/integrations/base_test.rb
|
109
|
+
- test/support/filter.rb
|
110
|
+
- test/support/model.rb
|
111
|
+
- test/test_helper.rb
|