categoryz3_forms 0.0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,6 @@
1
1
  = Categoryz3Forms
2
+ {<img src="https://secure.travis-ci.org/tscolari/categoryz3_forms.png" alt="Build Status" />}[http://travis-ci.org/tscolari/categoryz3_forms]
3
+ {<img src="https://codeclimate.com/badge.png" />}[https://codeclimate.com/github/tscolari/categoryz3_forms]
2
4
 
3
5
  Adds a form helper (categories_select) for using with Categoryz3.
4
6
 
@@ -0,0 +1,74 @@
1
+ module Categoryz3
2
+ class CategoriesSelect
3
+
4
+ # Initialize the category select
5
+ #
6
+ # context: is the actual template, from where helpers can be called
7
+ # object_name: the object_name for the form
8
+ # options: Extra options for the select tag and javascripts
9
+ #
10
+ def initialize(context, object_name, options = {})
11
+ normalize_options(options)
12
+ @context = context
13
+ @object_name = object_name
14
+ end
15
+
16
+ # Public: Returns the select tag html with javascript
17
+ #
18
+ def output
19
+ (select_tag + javascript_tag).html_safe
20
+ end
21
+
22
+ private
23
+
24
+
25
+ # Private: Returns the select_tag
26
+ #
27
+ def select_tag
28
+ @context.select_tag("#{@object_name}[categories_list][]",
29
+ @context.options_for_select(categories_collection, @object.categories_list.split(",")),
30
+ options[:html_options]
31
+ ).html_safe
32
+ end
33
+
34
+ # Private: Returns the javascript tag with configuration for the select2
35
+ #
36
+ def javascript_tag
37
+ "<script type=\"text/javascript\">
38
+ $(document).ready(function() { $('##{@object_name}_categories_list').select2({
39
+ placeholder: '#{@context.t('categoryz3.select_placeholder', default: 'Select a Category')}',
40
+ allowClear: true,
41
+ width: 'resolve'
42
+ #{max_select_option}
43
+ });} )
44
+ </script>".html_safe
45
+ end
46
+
47
+ # Private: Set the default options for the tag
48
+ #
49
+ def normalize_options(options)
50
+ options[:html_options] = {} unless options[:html_options]
51
+ options[:html_options].reverse_merge!({id: "#{@object_name}_categories_list", multiple: (options[:unique] ? false : 'multiple')})
52
+ @options = options
53
+ end
54
+
55
+ # Private: List all categories for to populate the options
56
+ #
57
+ def categories_collection
58
+ @categories ||= Categoryz3::Category.all.map { |category| category_to_option(category) }
59
+ end
60
+
61
+ # Private: Converts a category to a [category.path, id] format
62
+ #
63
+ def category_to_option(category)
64
+ [category.path.map(&:name).join('/'), category.id]
65
+ end
66
+
67
+ # Private: Adds the max categories that Select2 will let you select
68
+ #
69
+ def max_select_option
70
+ ", maximumSelectionSize: #{@options[:max_categories]}" if @options[:max_categories]
71
+ end
72
+
73
+ end
74
+ end
@@ -1,3 +1,4 @@
1
+ require 'categoryz3/categories_select'
1
2
  module Categoryz3
2
3
  module FormHelpers
3
4
 
@@ -18,38 +19,8 @@ module Categoryz3
18
19
  #
19
20
  #
20
21
  def categories_select(options = {})
21
- options = categories_normalize_options(options)
22
- out = @template.select_tag("#{@object_name}[categories_list][]", @template.options_for_select(categories_collection, @object.categories_list.split(",")),
23
- options[:html_options]
24
- ).html_safe
25
- out += "<script type=\"text/javascript\">
26
- $(document).ready(function() { $('##{@object_name}_categories_list').select2({
27
- placeholder: '#{@template.t('categoryz3.select_placeholder', default: 'Select a Category')}',
28
- allowClear: true,
29
- width: 'resolve'
30
- #{categories_max_select(options)}
31
- });} )
32
- </script>".html_safe
33
- end
34
-
35
- private
36
-
37
- def categories_normalize_options(options)
38
- options[:html_options] = {} unless options[:html_options]
39
- options[:html_options].reverse_merge!({id: "#{@object_name}_categories_list", multiple: (options[:unique] ? false : 'multiple')})
40
- options
41
- end
42
-
43
- def categories_collection
44
- @categories ||= Categoryz3::Category.all.map { |category| category_option_array(category) }
45
- end
46
-
47
- def category_option_array(category)
48
- [category.path.map(&:name).join('/'), category.id]
49
- end
50
-
51
- def categories_max_select(options)
52
- ", maximumSelectionSize: #{options[:max_categories]}" if options[:max_categories]
22
+ categories_select = CategoriesSelect.new(@template, @object_name, options)
23
+ categories_select.output
53
24
  end
54
25
 
55
26
  end
@@ -1,3 +1,3 @@
1
1
  module Categoryz3Forms
2
- VERSION = "0.0.1"
2
+ VERSION = "0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: categoryz3_forms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: '0.2'
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: 2012-10-07 00:00:00.000000000 Z
12
+ date: 2012-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -43,22 +43,6 @@ dependencies:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0.2'
46
- - !ruby/object:Gem::Dependency
47
- name: sqlite3
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
46
  - !ruby/object:Gem::Dependency
63
47
  name: rspec-rails
64
48
  requirement: !ruby/object:Gem::Requirement
@@ -131,6 +115,7 @@ executables: []
131
115
  extensions: []
132
116
  extra_rdoc_files: []
133
117
  files:
118
+ - lib/categoryz3/categories_select.rb
134
119
  - lib/categoryz3/form_helpers.rb
135
120
  - lib/categoryz3_forms/rails.rb
136
121
  - lib/categoryz3_forms/version.rb
@@ -159,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
144
  version: '0'
160
145
  segments:
161
146
  - 0
162
- hash: -3466529017128052204
147
+ hash: -1054542396351280195
163
148
  required_rubygems_version: !ruby/object:Gem::Requirement
164
149
  none: false
165
150
  requirements:
@@ -168,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
153
  version: '0'
169
154
  segments:
170
155
  - 0
171
- hash: -3466529017128052204
156
+ hash: -1054542396351280195
172
157
  requirements: []
173
158
  rubyforge_project:
174
159
  rubygems_version: 1.8.24