form_object 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in form_object.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ gem 'minitest'
8
+ gem 'turn'
@@ -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.
@@ -0,0 +1,32 @@
1
+ # FormObject
2
+
3
+ [![Build Status](https://travis-ci.org/saratovsource/form_object.png)](https://travis-ci.org/saratovsource/form_object)
4
+ [![Code Climate](https://codeclimate.com/badge.png)](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
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/lib/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
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
@@ -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
@@ -0,0 +1,9 @@
1
+ require "form_object/version"
2
+ require 'virtus'
3
+ require 'active_model'
4
+
5
+ module FormObject
6
+ autoload :Base, 'form_object/base'
7
+ autoload :Integrations, 'form_object/integrations'
8
+ # Your code goes here...
9
+ end
@@ -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,12 @@
1
+ module FormObject
2
+ module Integrations
3
+ module ActiveModel
4
+ include Base
5
+
6
+ def self.matching_ancestors
7
+ %w(ActiveModel ActiveModel::Observing ActiveModel::Validations)
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module FormObject
2
+ module Integrations
3
+ module ActiveRecord
4
+ include Base
5
+
6
+ def self.matching_ancestors
7
+ %w(ActiveRecord::Base)
8
+ end
9
+
10
+ end
11
+ end
12
+ 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,3 @@
1
+ module FormObject
2
+ VERSION = "0.0.1"
3
+ 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,12 @@
1
+ require 'test_helper'
2
+
3
+ class FormRelationTest < TestCase
4
+ def setup
5
+ @form = Filter.new(query: "find me")
6
+ end
7
+
8
+ def test_should_not_form_get_model
9
+ assert_respond_to @form, :model
10
+ assert @form.model.nil?
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class FormsCollectionTest < TestCase
4
+ def setup
5
+ @form = Filter.new(query: "find me")
6
+ end
7
+
8
+ def test_should_model_have_forms_collection
9
+
10
+ end
11
+ 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
@@ -0,0 +1,6 @@
1
+ class Filter < FormObject::Base
2
+ attribute :query, String
3
+ attribute :created_at, DateTime, default: Proc.new{ DateTime.now }
4
+
5
+ validates :query, presence: true
6
+ end
File without changes
@@ -0,0 +1,10 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'minitest/autorun'
4
+
5
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
6
+
7
+ MiniTest::Unit.autorun
8
+
9
+ class TestCase < MiniTest::Unit::TestCase
10
+ end
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