rails_admin_select2 0.0.1.pre → 0.0.2.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ <%- tags_value = field.value || '' %>
2
+ <%- tags_predefined = "[%s]" % field.tags.map{ |t| "\"#{t.to_s}\"" }.join(',') %>
3
+ <%= form.hidden_field(field.method_name, { :class => 'select2', :style => 'width:75%', :value => tags_value }) %>
4
+ <script type="text/javascript">
5
+ $(<%= "#{field.abstract_model.to_s.underscore}_#{field.method_name}" %>).select2({ tags: <%= tags_predefined.html_safe %> });
6
+ </script>
@@ -1,7 +1,7 @@
1
1
  module RailsAdminSelect2
2
2
  MAJOR = '0'
3
3
  MINOR = '0'
4
- PATCH = '1'
4
+ PATCH = '2'
5
5
  PRE = 'pre'
6
6
  VERSION = [ MAJOR, MINOR, PATCH, PRE ].join('.')
7
7
  end
@@ -8,34 +8,115 @@ module RailsAdmin
8
8
  module Fields
9
9
  module Types
10
10
  # Add support for select2 input fields to RailsAdmin.
11
+ #
12
+ # Select2 home with demos: http://ivaynberg.github.io/select2/.
11
13
  class Select2 < RailsAdmin::Config::Fields::Base
12
14
  RailsAdmin::Config::Fields::Types::register(self)
15
+ # For relations, render a (multi)select select2 field.
16
+ # For strings, render a hidden field for tagging (if :tags is an array).
13
17
  def initialize(parent, name, properties)
14
18
  super(parent, name, properties)
15
- @meta = abstract_model.model.reflect_on_association(name)
16
- if not [:belongs_to, :has_one, :has_many, :has_and_belongs_to_many].include? @meta.macro
17
- raise RuntimeError.new('Cannot render select2 for association %s#%s of type %s.' % [@meta.class_name, @meta.name, @meta.macro])
18
- end
19
+ # Mongoid allows field aliases (:as).
20
+ # Map both names and aliases to field meta information.
21
+ @fields = Hash[*(abstract_model.model.fields.map{|k,v| [[k.to_sym,v],[v.options[:as].try(:to_sym),v]]}.flatten)].delete_if{|k,v| k.nil?}
22
+ @relation = abstract_model.model.reflect_on_association(name)
23
+ @arity = {
24
+ :embeds_one => :one,
25
+ :embedded_in => :one,
26
+ :embeds_many => :many,
27
+ :belongs_to => :one,
28
+ :has_one => :one,
29
+ :has_many => :many,
30
+ :has_and_belongs_to_many => :many
31
+ }
32
+ end
33
+ # If tags have been supplied, render a select2 field for tagging.
34
+ # See http://ivaynberg.github.io/select2/#tags.
35
+ #
36
+ # The tags supplied will be available as choices in the tagging field,
37
+ # new tags can be added.
38
+ #
39
+ # To render a tagging field without predefined choices, supply []
40
+ # to tags, like so:
41
+ #
42
+ # config.model "Model" do
43
+ # edit do
44
+ # field do
45
+ # tags []
46
+ # end
47
+ # end
48
+ # end
49
+ #
50
+ # If no tags have been supplied, a collection of associated objects
51
+ # will be rendered for selection. See the :collection instance option.
52
+ #
53
+ # To disable tagging (the default) explicitly, set tags to nil.
54
+ register_instance_option(:tags) do
55
+ tagging? ? [] : nil
19
56
  end
20
57
  register_instance_option(:partial) do
21
- :form_select2
58
+ tagging? ? :form_select2_tags : :form_select2
22
59
  end
60
+ # If true, allow selecting multiple associated objects.
61
+ #
62
+ # This will be true, if the association macro is one of
63
+ # :has_many, :has_and_belongs_to_many, or :embeds_many.
64
+ #
65
+ # Raise a RuntimeError if tagging? has been enabled.
23
66
  register_instance_option(:multiple?) do
24
- [:has_many, :has_and_belongs_to_many].include? @meta.macro
67
+ if tagging?
68
+ raise RuntimeError.new "cannot render select2 because #{abstract_model.model.to_s}##{@name} does not define a relation"
69
+ end
70
+ @arity[@relation.macro] == :many
25
71
  end
72
+ # Per default, assemble a collection of all objects for this
73
+ # association by class name.
74
+ #
75
+ # For instance, if the association to render is article.article_group,
76
+ # the collection used for the select2 field will contain all ArticleGroups.
77
+ #
78
+ # If :tags have been supplied, a tagging field will be rendered
79
+ # and no collection is required/will be assembled.
26
80
  register_instance_option(:collection) do
27
- @meta.class_name.safe_constantize.all.map{ |object| [ object.name, object.id ] }
81
+ return nil if tagging?
82
+ @relation.class_name.safe_constantize.all.map{ |object| [ object.name, object.id ] }
28
83
  end
29
- # For Mongoid, the method_name required is <name>_id[s],
30
- # e.g. children_ids or parent_id.
84
+ # For Mongoid, the method_name for referenced associations is <name>_id[s],
85
+ # e.g. children_ids or parent_id; for embedded documents it is <name> or
86
+ # <name>s, respectively.
31
87
  #
32
- # To construct method_name, start with the association name and
33
- # append '_id' or '_ids' for :belongs_to and all other types
34
- # of associations, respectively.
88
+ # Return #{name} for embedded documents, #{name}_id for referenced documents;
89
+ # append 's', if the association references multiple documents.
35
90
  #
36
- # TODO Does this work in AR, too?
91
+ # TODO How does this work in AR?
37
92
  def method_name
38
- '%s_%s' % [ @meta.name.to_s.singularize, multiple? ? 'ids' : 'id' ]
93
+ return @name if tagging?
94
+ embedded = [ :embeds_one, :embeds_many, :embedded_in ].include? @relation.macro
95
+ m = embedded ? @relation.name.to_s.singularize : "#{@relation.name.to_s.singularize}_id"
96
+ multiple? ? m.pluralize : m
97
+ end
98
+
99
+ private
100
+
101
+ # Return true, if this tagging should be enabled for this field.
102
+ #
103
+ # For tagging to be enabled, the type of the field must be
104
+ # Mongoid::Fields::Standard and the type of the attribute
105
+ # must be String.
106
+ #
107
+ # Tagging is not supported for relation fields (associations).
108
+ def tagging?
109
+ # The name of a relation and the name of the field
110
+ # storing values for the relation differ (<name>_id[s] vs <name>).
111
+ # If @fields does not contain a value for @name,
112
+ # this field is a relation and tagging is not supported.
113
+ return false unless @fields.has_key?(@name)
114
+ # Tagging requires a field of type Mongoid::Fields::Standard
115
+ # with an attribute of type String.
116
+ klass = @fields[@name].class
117
+ type = @fields[@name].options[:type]
118
+ # TODO allow type of String and Array?
119
+ klass == Mongoid::Fields::Standard && [ ::String ].include?(type)
39
120
  end
40
121
  end
41
122
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_select2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre
4
+ version: 0.0.2.pre
5
5
  prerelease: 6
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-09-30 00:00:00.000000000 Z
12
+ date: 2013-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails_admin
@@ -50,6 +50,7 @@ executables: []
50
50
  extensions: []
51
51
  extra_rdoc_files: []
52
52
  files:
53
+ - app/views/rails_admin/main/_form_select2_tags.html.erb
53
54
  - app/views/rails_admin/main/_form_select2.html.erb
54
55
  - lib/rails_admin_select2/version.rb
55
56
  - lib/rails_admin_select2/engine.rb
@@ -68,6 +69,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
69
  - - ! '>='
69
70
  - !ruby/object:Gem::Version
70
71
  version: '0'
72
+ segments:
73
+ - 0
74
+ hash: 652118273
71
75
  required_rubygems_version: !ruby/object:Gem::Requirement
72
76
  none: false
73
77
  requirements:
@@ -81,4 +85,3 @@ signing_key:
81
85
  specification_version: 3
82
86
  summary: Select2 for rails_admin
83
87
  test_files: []
84
- has_rdoc: