mongoid_form 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_form.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ivan Teplyakov
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,75 @@
1
+ # MongoidForm
2
+
3
+ It makes your life easy when you develop forms and your app use [mongoid](https://github.com/mongoid/mongoid).
4
+
5
+ ## Cool features
6
+
7
+ ### Works with localized fields!
8
+
9
+ ``` ruby
10
+ # app/models/category.rb
11
+ field :name, localize: true
12
+
13
+ # views/category/_form.rb
14
+ = form_for @category, wrapper: :default, html: { class: 'form-horizontal' } do |f|
15
+ = f.localized :name
16
+ # generates input fields for each locales in your config.i18n.available_locales
17
+ ...
18
+ ```
19
+
20
+ ### Shows asterisk for required fields!
21
+
22
+ ``` ruby
23
+ # config/initializers/mongoid_form_config.rb
24
+ ...
25
+ # this option add before label text "<abbr title=\"required field\">*</abbr>" to required fields
26
+ add_if_required :abbr, '*', title: I18n::t('required')
27
+ ...
28
+ ```
29
+
30
+ ## Installation
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ gem 'mongoid_form'
35
+
36
+ And then execute:
37
+
38
+ $ bundle
39
+
40
+ Run installer for generate sample config:
41
+
42
+ $ rails generate mongoid_form:install
43
+
44
+ ## Usage
45
+
46
+ ``` haml
47
+ = form_for @category, wrapper: :backend, html: { class: 'form-horizontal' } do |f|
48
+ = f.error_notification
49
+
50
+ = f.localized :name
51
+ = f.input :alias # text_field by default
52
+ = f.input :visible, :checkbox # pass type of field
53
+ ```
54
+
55
+ ### Types of fields supported:
56
+
57
+ * ```:text```
58
+ * ```:password```
59
+ * ```:select```
60
+ * ```:number```
61
+ * ```:text_area```
62
+ * ```:check_box```
63
+ * ```:hidden```
64
+ * ```:file```
65
+
66
+ If type is ```:hidden``` it will not be wrapped!
67
+
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ require 'rails/generators'
2
+
3
+ module MongoidForm
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ desc "MongoidForm installation generator"
9
+
10
+ def install
11
+ say "MongoidForm resources will be installed...", :cyan
12
+
13
+ copy_file 'config.rb', File.join('config/initializers', 'mongoid_form_config.rb')
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ MongoidForm.setup do |config|
2
+
3
+ config.wrapper :default do
4
+ alert_error_wrapper :div, class: 'alert alert-error'
5
+ main_error_i18n_key 'errors.form.error_notification'
6
+ group_wrapper :div, class: 'control-group'
7
+ group_error_class 'error'
8
+ label_options class: 'control-label'
9
+ add_if_required :abbr, '*', title: I18n::t('required')
10
+ error_block :span, class: 'help-inline'
11
+ input_wrapper :div, class: 'controls'
12
+ end
13
+
14
+ end
@@ -0,0 +1,153 @@
1
+ module MongoidForm
2
+ class FormBuilder < ActionView::Helpers::FormBuilder
3
+
4
+ SKIP_WRAP_FOR = [:hidden]
5
+
6
+ def localized_fields(field, options = {}, &block)
7
+ field_name = "#{field}_translations"
8
+ object = @object.send(field_name).try(:empty?) ? nil : @object.send(field_name)
9
+ name = "#{object_name}[#{field_name}]"
10
+
11
+ @template.fields_for(name, object, &block).html_safe
12
+ end
13
+
14
+ def localized_text_field(attribute, options = {})
15
+ attribute, options = value_field(attribute, options)
16
+ text_field(attribute, options).html_safe
17
+ end
18
+
19
+ def error_notification
20
+ if any_errors?
21
+ alert { I18n::t(*wrapper.main_error_i18n_key, model: @object.class.model_name.human, count: @object.errors.size) }
22
+ end
23
+ end
24
+
25
+ def error_message_for(name)
26
+ if any_errors? && has_error?(name)
27
+ alert { get_error(name) }
28
+ end
29
+ end
30
+
31
+ def required?(method)
32
+ @object && @object.class.validators_on(method).map(&:class).include?(Mongoid::Validations::PresenceValidator)
33
+ end
34
+
35
+ def any_errors?
36
+ @object && @object.respond_to?(:errors) && @object.errors.present?
37
+ end
38
+
39
+ def has_error?(name)
40
+ return false unless any_errors?
41
+ @object.errors.messages[name].present?
42
+ end
43
+
44
+ private
45
+
46
+ def factory(type, name, options = {})
47
+ input = case type
48
+ when :text
49
+ text_field name, options
50
+ when :password
51
+ password_field name, options
52
+ when :select
53
+ options[:collection] ||= []
54
+ select name, options[:collection], options
55
+ when :number
56
+ number_field name, options
57
+ when :text_area
58
+ text_area name, options
59
+ when :check_box
60
+ check_box name, options
61
+ when :hidden
62
+ hidden_field name, options
63
+ when :file
64
+ file_field name, options
65
+ end
66
+ (SKIP_WRAP_FOR.include?(type) || wrapper.nil?) ? input : wrap_field(input, name)
67
+ end
68
+
69
+ def wrap_field(input, name)
70
+ label = label(name, *wrapper.label_options) do
71
+ asterisk(name) + @object.class.human_attribute_name(name)
72
+ end
73
+
74
+ wrap_group(input, name, label)
75
+ end
76
+
77
+ def wrap_localized_fields(builder)
78
+ name = builder.object_name.scan(/\[(.*)_translations\]/).join.to_sym
79
+ result = ''
80
+ I18n.available_locales.each do |locale|
81
+ field = builder.object_name.scan(/(.*)\[(.*)_translations\]/).join('.')
82
+ flag = wrapper.flag_for_localized ? wrap('', [:div, class: "flag flags-#{locale.to_s}"]) : ''
83
+ label = builder.label locale.to_sym, *wrapper.label_options do
84
+ asterisk(name) + I18n::t("mongoid.attributes.#{field}") + flag
85
+ end
86
+ input = builder.localized_text_field locale.to_sym
87
+
88
+ result << wrap_group(input, name, label)
89
+ end
90
+ result.html_safe
91
+ end
92
+
93
+ def wrapper
94
+ @options[:wrapper] ? MongoidForm.wrappers(@options[:wrapper]) : nil
95
+ end
96
+
97
+ def wrap(content, options)
98
+ @template.content_tag *options do
99
+ content
100
+ end
101
+ end
102
+
103
+ def asterisk(name)
104
+ required?(name) && wrapper.add_if_required ? @template.content_tag(*wrapper.add_if_required) + ' ' : ''
105
+ end
106
+
107
+ def error(name)
108
+ has_error?(name) ? wrap(get_error(name), wrapper.error_block) : ''
109
+ end
110
+
111
+ def get_error(name)
112
+ error = get_errors(name)
113
+ error = error.first if error.is_a?(Array)
114
+ error
115
+ end
116
+
117
+ def get_errors(name)
118
+ @object.errors.messages[name]
119
+ end
120
+
121
+ def input_wrapped(input, name)
122
+ wrapper.input_wrapper ? wrap((input + error(name)), wrapper.input_wrapper) : (input + error(name))
123
+ end
124
+
125
+ def wrap_group(input, name, label)
126
+ if wrapper.group_wrapper
127
+ group_wrapper = wrapper.group_wrapper.dup
128
+
129
+ if has_error?(name) && wrapper.group_error_class
130
+ extracted = group_wrapper.extract_options!
131
+ options = extracted.merge(class: extracted[:class] + " " + wrapper.group_error_class.first)
132
+ group_wrapper << options
133
+ end
134
+
135
+ wrap (label + input_wrapped(input, name)), group_wrapper
136
+ else
137
+ (label + input_wrapped(input, name))
138
+ end
139
+ end
140
+
141
+ def alert(&block)
142
+ inner = yield.html_safe
143
+ wrap inner, wrapper.alert_error_wrapper
144
+ end
145
+
146
+ def value_field(attribute, options)
147
+ value = @object ? @object[attribute.to_s] : nil
148
+ options = options.merge(value: value)
149
+ return attribute, options
150
+ end
151
+
152
+ end
153
+ end
@@ -0,0 +1,18 @@
1
+ module MongoidForm
2
+ module Helpers
3
+ module FormHelper
4
+
5
+ def localized(name)
6
+ localized_fields(name) { |lf| @template.concat wrap_localized_fields(lf) }
7
+ end
8
+
9
+ def input(name, *args)
10
+ type = args[0]
11
+ options = args.extract_options!
12
+ type ||= :text
13
+ factory type, name, options
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module MongoidForm
2
+ module Helpers
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :FormHelper
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidForm
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ module MongoidForm
2
+ class WrapperConfig
3
+
4
+ def initialize(&block)
5
+ instance_eval &block
6
+ end
7
+
8
+ def method_missing meth, *args, &block
9
+ if instance_variable_defined? "@#{meth}"
10
+ instance_variable_get "@#{meth}"
11
+ else
12
+ instance_variable_set "@#{meth}", args
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,42 @@
1
+ require 'mongoid'
2
+ require 'action_view'
3
+ require "active_support/all"
4
+
5
+ module MongoidForm
6
+ extend ActiveSupport::Autoload
7
+
8
+ autoload :FormBuilder
9
+ autoload :Helpers
10
+ autoload :WrapperConfig
11
+
12
+ mattr_accessor :default_wrapper
13
+ @@default_wrapper = :default
14
+ @@wrappers = {}
15
+
16
+ def self.setup
17
+ yield self
18
+ end
19
+
20
+ def self.wrapper(name, &block)
21
+ if block_given?
22
+ name ||= :default
23
+ @@wrappers[name.to_sym] = MongoidForm::WrapperConfig.new(&block)
24
+ else
25
+ @@wrappers
26
+ end
27
+ end
28
+
29
+ def self.wrappers(name)
30
+ name ||= :default
31
+ @@wrappers[name.to_sym] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
32
+ end
33
+
34
+ class WrapperNotFound < StandardError
35
+ end
36
+
37
+ end
38
+
39
+ ActionView::Base.default_form_builder = MongoidForm::FormBuilder
40
+ ActionView::Base.field_error_proc = Proc.new { |html_tag, instance| "#{html_tag}".html_safe }
41
+
42
+ ActionView::Helpers::FormBuilder.send :include, MongoidForm::Helpers::FormHelper
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid_form/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid_form"
8
+ spec.version = MongoidForm::VERSION
9
+ spec.authors = ["Ivan Teplyakov"]
10
+ spec.email = ["exreanimator@gmail.com"]
11
+ spec.description = %q{It makes your life easy when you develop forms and your app use mongoid}
12
+ spec.summary = %q{Form builder for apps which use mongoid}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'mongoid', '>= 2.4'
22
+ spec.add_dependency 'actionpack', '>= 3.1'
23
+ spec.add_dependency 'activesupport', '>= 3.1'
24
+ spec.add_dependency 'rails', '>= 3.1'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ivan Teplyakov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.4'
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: '2.4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: actionpack
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '3.1'
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: '3.1'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '3.1'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '3.1'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '3.1'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '1.3'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '1.3'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: It makes your life easy when you develop forms and your app use mongoid
111
+ email:
112
+ - exreanimator@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - LICENSE.txt
120
+ - README.md
121
+ - Rakefile
122
+ - lib/generators/mongoid_form/install_generator.rb
123
+ - lib/generators/mongoid_form/templates/config.rb
124
+ - lib/mongoid_form.rb
125
+ - lib/mongoid_form/form_builder.rb
126
+ - lib/mongoid_form/helpers.rb
127
+ - lib/mongoid_form/helpers/form_helper.rb
128
+ - lib/mongoid_form/version.rb
129
+ - lib/mongoid_form/wrapper_config.rb
130
+ - mongoid_form.gemspec
131
+ homepage: ''
132
+ licenses:
133
+ - MIT
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.25
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: Form builder for apps which use mongoid
156
+ test_files: []