mongoid_form 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.0.4
4
+ * Added support for associations
5
+ * Added helper radios_from_collection
6
+ * Type of field passing to a label class for better customization
7
+ * Fixed bug with passing params for input
8
+
3
9
  ## 0.0.3
4
10
  * Localized fields for text_field and text_area
5
11
  * Fix issue with flag block rendering if field is not required
data/README.md CHANGED
@@ -9,18 +9,25 @@ It makes your life easy when you develop forms and your app use [mongoid](https:
9
9
  ``` ruby
10
10
  # app/models/category.rb
11
11
  field :name, localize: true
12
+ field :content, localize: true
12
13
 
13
14
  # views/category/_form.rb
14
15
  = form_for @category, wrapper: :default, html: { class: 'form-horizontal' } do |f|
15
- = f.localized :name
16
+ # text_field type by default
16
17
  # generates input fields for each locales in your config.i18n.available_locales
18
+ = f.localized :name
19
+
20
+ # pass text_area type and some class for example
21
+ # generates text_area fields for each locales in your config.i18n.available_locales
22
+ = f.localized :content, :text_area, class: "tinymce"
23
+
17
24
  ...
18
25
  ```
19
26
 
20
27
  ### Can show flag block with each localized fields
21
28
 
29
+ ``config/initializers/mongoid_form_config.rb``
22
30
  ``` ruby
23
- # config/initializers/mongoid_form_config.rb
24
31
  ...
25
32
  # this option add after label text "<div class=\"flag flags-en\" />" to each locale fo localized fields
26
33
  # you should styling .flag and .flags-en (.flags-ru etc.) in your css.
@@ -30,8 +37,8 @@ It makes your life easy when you develop forms and your app use [mongoid](https:
30
37
 
31
38
  ### Shows asterisk for required fields!
32
39
 
40
+ ``config/initializers/mongoid_form_config.rb``
33
41
  ``` ruby
34
- # config/initializers/mongoid_form_config.rb
35
42
  ...
36
43
  # this option add before label text "<abbr title=\"required field\">*</abbr>" to required fields
37
44
  add_if_required :abbr, '*', title: I18n::t('required')
@@ -55,12 +62,19 @@ Run installer for generate sample config:
55
62
  ## Usage
56
63
 
57
64
  ``` haml
58
- = form_for @category, wrapper: :backend, html: { class: 'form-horizontal' } do |f|
65
+ = form_for @category, wrapper: :default, html: { class: 'form-horizontal' } do |f|
59
66
  = f.error_notification
60
67
 
61
68
  = f.localized :name
62
- = f.input :alias # text_field by default
63
- = f.input :visible, :checkbox # pass type of field
69
+ = f.input :alias # text_field by default
70
+ = f.input :visible, :check_box # passed type of field
71
+
72
+ = f.association :location do |lf|
73
+ = f.input :country # -> category[location_attributes][country]
74
+ = f.input :city # -> category[location_attributes][city]
75
+
76
+ = f.radios_from_collection :gender, collection: { 1 => t('genders.male'), 2 => t('genders.female') }
77
+
64
78
  ```
65
79
 
66
80
  ### Types of fields supported:
@@ -85,15 +99,17 @@ All options for this time:
85
99
  MongoidForm.setup do |config|
86
100
 
87
101
  config.wrapper :default do
88
- alert_error_wrapper :div, class: 'alert alert-error'
89
- main_error_i18n_key 'errors.form.error_notification'
90
- group_wrapper :div, class: 'control-group'
91
- group_error_class 'error'
92
- label_options class: 'control-label'
93
- add_if_required :abbr, '*', title: I18n::t('required')
94
- error_block :span, class: 'help-inline'
95
- input_wrapper :div, class: 'controls'
96
- flag_for_localized true
102
+ alert_error_wrapper :div, class: 'alert alert-error' # wrapper for form main error
103
+ main_error_i18n_key 'errors.form.error_notification' # i18n key for form main error
104
+ group_wrapper :div, class: 'control-group' # it wrap group of label + input
105
+ group_error_class 'error' # that class will added to group wrapper if errors for field
106
+ label_options class: 'control-label' # options for label field
107
+ add_if_required :abbr, '*', title: I18n::t('required') # appears before label text if field is required
108
+ error_block :span, class: 'help-inline' # element containing error message, appears after input
109
+ input_wrapper :div, class: 'controls' # wrapper element for each input
110
+ flag_for_localized true # show block with class "flag flags-#{locale}" after label text
111
+ # of localized fields
112
+ radios_wrapper :div, class: 'radios' # wrapper for radios group
97
113
  end
98
114
 
99
115
  end
@@ -10,6 +10,7 @@ MongoidForm.setup do |config|
10
10
  error_block :span, class: 'help-inline'
11
11
  input_wrapper :div, class: 'controls'
12
12
  flag_for_localized true
13
+ radios_wrapper :div, class: 'radios'
13
14
  end
14
15
 
15
16
  end
@@ -12,6 +12,7 @@ module MongoidForm
12
12
  end
13
13
 
14
14
  def localized_field(type, attribute, options = {})
15
+ @type = type
15
16
  attribute, options = value_field(attribute, options)
16
17
  case type
17
18
  when :text
@@ -51,6 +52,7 @@ module MongoidForm
51
52
  private
52
53
 
53
54
  def factory(type, name, options = {})
55
+ @type = type
54
56
  input = case type
55
57
  when :text
56
58
  text_field name, options
@@ -74,7 +76,13 @@ module MongoidForm
74
76
  end
75
77
 
76
78
  def wrap_field(input, name)
77
- label = label(name, *wrapper.label_options) do
79
+ label_options = wrapper.label_options.dup
80
+ extracted = label_options.extract_options!
81
+ extracted_class = extracted[:class] ? extracted[:class] + ' ' : ''
82
+ options = extracted.merge(class: extracted_class + @type.to_s)
83
+ label_options << options
84
+
85
+ label = label(name, *label_options) do
78
86
  asterisk(name) + @object.class.human_attribute_name(name)
79
87
  end
80
88
 
@@ -139,13 +147,14 @@ module MongoidForm
139
147
 
140
148
  if has_error?(name) && wrapper.group_error_class
141
149
  extracted = group_wrapper.extract_options!
142
- options = extracted.merge(class: extracted[:class] + " " + wrapper.group_error_class.first)
150
+ extracted_class = extracted[:class] ? extracted[:class] + ' ' : ''
151
+ options = extracted.merge(class: extracted_class + wrapper.group_error_class.first)
143
152
  group_wrapper << options
144
153
  end
145
154
 
146
155
  wrap (label + input_wrapped(input, name)), group_wrapper
147
156
  else
148
- (label + input_wrapped(input, name))
157
+ label + input_wrapped(input, name)
149
158
  end
150
159
  end
151
160
 
@@ -2,20 +2,41 @@ module MongoidForm
2
2
  module Helpers
3
3
  module FormHelper
4
4
 
5
+ def input(name, *args)
6
+ type, options = get_options(args)
7
+ factory type, name, options
8
+ end
9
+
5
10
  def localized(name, *args)
6
11
  type, options = get_options(args)
7
12
  localized_fields(name) { |lf| @template.concat wrap_localized_fields(lf, type, options) }
8
13
  end
9
14
 
10
- def input(name, *args)
11
- type, options = get_options(args)
12
- factory type, name, options
15
+ def association(*args, &block)
16
+ options = args.extract_options!
17
+ options[:wrapper] = self.options[:wrapper] if options[:wrapper].nil?
18
+ options[:builder] ||= MongoidForm::FormBuilder
19
+
20
+ fields_for(*(args << options), &block)
21
+ end
22
+
23
+ def radios_from_collection(name, *args)
24
+ options = args.extract_options!
25
+ raise "Collection for radios not passed" unless options[:collection]
26
+ result = ''
27
+ options[:collection].each do |value, text|
28
+ result << radio_button(name, value) + label(name, text)
29
+ end
30
+ radios_block = wrapper.radios_wrapper.present? ? wrap(result.html_safe, wrapper.radios_wrapper) : result.html_safe
31
+ wrap_field(radios_block, name)
13
32
  end
14
33
 
15
34
  private
16
35
 
17
36
  def get_options(args)
18
- type = args[0]
37
+ if args.present?
38
+ type = args.first unless args.first.is_a?(Hash)
39
+ end
19
40
  options = args.extract_options!
20
41
  type ||= :text
21
42
  return type, options
@@ -1,3 +1,3 @@
1
1
  module MongoidForm
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = MongoidForm::VERSION
9
9
  spec.authors = ["Ivan Teplyakov"]
10
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}
11
+ spec.description = %q{Form builder for apps which use mongoid}
12
+ spec.summary = %q{It makes your life easy when you develop forms and your apps uses mongoid}
13
13
  spec.homepage = "https://github.com/ExReanimator/mongoid_form"
14
14
  spec.license = "MIT"
15
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-21 00:00:00.000000000 Z
12
+ date: 2013-03-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -107,7 +107,7 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
- description: It makes your life easy when you develop forms and your app use mongoid
110
+ description: Form builder for apps which use mongoid
111
111
  email:
112
112
  - exreanimator@gmail.com
113
113
  executables: []
@@ -153,5 +153,5 @@ rubyforge_project:
153
153
  rubygems_version: 1.8.25
154
154
  signing_key:
155
155
  specification_version: 3
156
- summary: Form builder for apps which use mongoid
156
+ summary: It makes your life easy when you develop forms and your apps uses mongoid
157
157
  test_files: []