activeadmin_addons_rails6 1.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (183) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +24 -0
  4. data/app/assets/images/fileicons/file_extension_3gp.png +0 -0
  5. data/app/assets/images/fileicons/file_extension_7z.png +0 -0
  6. data/app/assets/images/fileicons/file_extension_ace.png +0 -0
  7. data/app/assets/images/fileicons/file_extension_ai.png +0 -0
  8. data/app/assets/images/fileicons/file_extension_aif.png +0 -0
  9. data/app/assets/images/fileicons/file_extension_aiff.png +0 -0
  10. data/app/assets/images/fileicons/file_extension_amr.png +0 -0
  11. data/app/assets/images/fileicons/file_extension_asf.png +0 -0
  12. data/app/assets/images/fileicons/file_extension_asx.png +0 -0
  13. data/app/assets/images/fileicons/file_extension_bat.png +0 -0
  14. data/app/assets/images/fileicons/file_extension_bin.png +0 -0
  15. data/app/assets/images/fileicons/file_extension_bmp.png +0 -0
  16. data/app/assets/images/fileicons/file_extension_bup.png +0 -0
  17. data/app/assets/images/fileicons/file_extension_cab.png +0 -0
  18. data/app/assets/images/fileicons/file_extension_cbr.png +0 -0
  19. data/app/assets/images/fileicons/file_extension_cda.png +0 -0
  20. data/app/assets/images/fileicons/file_extension_cdl.png +0 -0
  21. data/app/assets/images/fileicons/file_extension_cdr.png +0 -0
  22. data/app/assets/images/fileicons/file_extension_chm.png +0 -0
  23. data/app/assets/images/fileicons/file_extension_dat.png +0 -0
  24. data/app/assets/images/fileicons/file_extension_divx.png +0 -0
  25. data/app/assets/images/fileicons/file_extension_dll.png +0 -0
  26. data/app/assets/images/fileicons/file_extension_dmg.png +0 -0
  27. data/app/assets/images/fileicons/file_extension_doc.png +0 -0
  28. data/app/assets/images/fileicons/file_extension_docx.png +0 -0
  29. data/app/assets/images/fileicons/file_extension_dss.png +0 -0
  30. data/app/assets/images/fileicons/file_extension_dvf.png +0 -0
  31. data/app/assets/images/fileicons/file_extension_dwg.png +0 -0
  32. data/app/assets/images/fileicons/file_extension_eml.png +0 -0
  33. data/app/assets/images/fileicons/file_extension_eps.png +0 -0
  34. data/app/assets/images/fileicons/file_extension_exe.png +0 -0
  35. data/app/assets/images/fileicons/file_extension_fla.png +0 -0
  36. data/app/assets/images/fileicons/file_extension_flv.png +0 -0
  37. data/app/assets/images/fileicons/file_extension_gif.png +0 -0
  38. data/app/assets/images/fileicons/file_extension_gz.png +0 -0
  39. data/app/assets/images/fileicons/file_extension_hqx.png +0 -0
  40. data/app/assets/images/fileicons/file_extension_htm.png +0 -0
  41. data/app/assets/images/fileicons/file_extension_html.png +0 -0
  42. data/app/assets/images/fileicons/file_extension_ifo.png +0 -0
  43. data/app/assets/images/fileicons/file_extension_indd.png +0 -0
  44. data/app/assets/images/fileicons/file_extension_iso.png +0 -0
  45. data/app/assets/images/fileicons/file_extension_jar.png +0 -0
  46. data/app/assets/images/fileicons/file_extension_jpeg.png +0 -0
  47. data/app/assets/images/fileicons/file_extension_jpg.png +0 -0
  48. data/app/assets/images/fileicons/file_extension_lnk.png +0 -0
  49. data/app/assets/images/fileicons/file_extension_log.png +0 -0
  50. data/app/assets/images/fileicons/file_extension_m4a.png +0 -0
  51. data/app/assets/images/fileicons/file_extension_m4b.png +0 -0
  52. data/app/assets/images/fileicons/file_extension_m4p.png +0 -0
  53. data/app/assets/images/fileicons/file_extension_m4v.png +0 -0
  54. data/app/assets/images/fileicons/file_extension_mcd.png +0 -0
  55. data/app/assets/images/fileicons/file_extension_mdb.png +0 -0
  56. data/app/assets/images/fileicons/file_extension_mid.png +0 -0
  57. data/app/assets/images/fileicons/file_extension_mov.png +0 -0
  58. data/app/assets/images/fileicons/file_extension_mp2.png +0 -0
  59. data/app/assets/images/fileicons/file_extension_mp3.png +0 -0
  60. data/app/assets/images/fileicons/file_extension_mp4.png +0 -0
  61. data/app/assets/images/fileicons/file_extension_mpeg.png +0 -0
  62. data/app/assets/images/fileicons/file_extension_mpg.png +0 -0
  63. data/app/assets/images/fileicons/file_extension_msi.png +0 -0
  64. data/app/assets/images/fileicons/file_extension_mswmm.png +0 -0
  65. data/app/assets/images/fileicons/file_extension_ogg.png +0 -0
  66. data/app/assets/images/fileicons/file_extension_pdf.png +0 -0
  67. data/app/assets/images/fileicons/file_extension_png.png +0 -0
  68. data/app/assets/images/fileicons/file_extension_pps.png +0 -0
  69. data/app/assets/images/fileicons/file_extension_ppt.png +0 -0
  70. data/app/assets/images/fileicons/file_extension_pptx.png +0 -0
  71. data/app/assets/images/fileicons/file_extension_ps.png +0 -0
  72. data/app/assets/images/fileicons/file_extension_psd.png +0 -0
  73. data/app/assets/images/fileicons/file_extension_pst.png +0 -0
  74. data/app/assets/images/fileicons/file_extension_ptb.png +0 -0
  75. data/app/assets/images/fileicons/file_extension_pub.png +0 -0
  76. data/app/assets/images/fileicons/file_extension_qbb.png +0 -0
  77. data/app/assets/images/fileicons/file_extension_qbw.png +0 -0
  78. data/app/assets/images/fileicons/file_extension_qxd.png +0 -0
  79. data/app/assets/images/fileicons/file_extension_ram.png +0 -0
  80. data/app/assets/images/fileicons/file_extension_rar.png +0 -0
  81. data/app/assets/images/fileicons/file_extension_rm.png +0 -0
  82. data/app/assets/images/fileicons/file_extension_rmvb.png +0 -0
  83. data/app/assets/images/fileicons/file_extension_rtf.png +0 -0
  84. data/app/assets/images/fileicons/file_extension_sea.png +0 -0
  85. data/app/assets/images/fileicons/file_extension_ses.png +0 -0
  86. data/app/assets/images/fileicons/file_extension_sit.png +0 -0
  87. data/app/assets/images/fileicons/file_extension_sitx.png +0 -0
  88. data/app/assets/images/fileicons/file_extension_ss.png +0 -0
  89. data/app/assets/images/fileicons/file_extension_swf.png +0 -0
  90. data/app/assets/images/fileicons/file_extension_tgz.png +0 -0
  91. data/app/assets/images/fileicons/file_extension_thm.png +0 -0
  92. data/app/assets/images/fileicons/file_extension_tif.png +0 -0
  93. data/app/assets/images/fileicons/file_extension_tmp.png +0 -0
  94. data/app/assets/images/fileicons/file_extension_torrent.png +0 -0
  95. data/app/assets/images/fileicons/file_extension_ttf.png +0 -0
  96. data/app/assets/images/fileicons/file_extension_txt.png +0 -0
  97. data/app/assets/images/fileicons/file_extension_unknown.png +0 -0
  98. data/app/assets/images/fileicons/file_extension_vcd.png +0 -0
  99. data/app/assets/images/fileicons/file_extension_vob.png +0 -0
  100. data/app/assets/images/fileicons/file_extension_wav.png +0 -0
  101. data/app/assets/images/fileicons/file_extension_wma.png +0 -0
  102. data/app/assets/images/fileicons/file_extension_wmv.png +0 -0
  103. data/app/assets/images/fileicons/file_extension_wps.png +0 -0
  104. data/app/assets/images/fileicons/file_extension_xls.png +0 -0
  105. data/app/assets/images/fileicons/file_extension_xlsx.png +0 -0
  106. data/app/assets/images/fileicons/file_extension_xpi.png +0 -0
  107. data/app/assets/images/fileicons/file_extension_zip.png +0 -0
  108. data/app/assets/images/material/icons/keyboard_arrow_down.svg +4 -0
  109. data/app/assets/images/material/icons/keyboard_arrow_left.svg +4 -0
  110. data/app/assets/images/material/icons/keyboard_arrow_right.svg +4 -0
  111. data/app/assets/images/material/icons/keyboard_arrow_up.svg +4 -0
  112. data/app/assets/images/material/icons/today.svg +4 -0
  113. data/app/assets/javascripts/activeadmin_addons/all.js +753 -0
  114. data/app/assets/stylesheets/activeadmin_addons/addons/interactive_select_tag.scss +30 -0
  115. data/app/assets/stylesheets/activeadmin_addons/addons/material-datepicker.scss +173 -0
  116. data/app/assets/stylesheets/activeadmin_addons/addons/material-toggle_bool.scss +52 -0
  117. data/app/assets/stylesheets/activeadmin_addons/addons/toggle_bool.scss +52 -0
  118. data/app/assets/stylesheets/activeadmin_addons/all.scss +19 -0
  119. data/app/assets/stylesheets/activeadmin_addons/imports/jquery-datepicker.scss +1 -0
  120. data/app/assets/stylesheets/activeadmin_addons/imports/webpack/jquery-datepicker.scss +1 -0
  121. data/app/assets/stylesheets/activeadmin_addons/inputs/color-picker.scss +4 -0
  122. data/app/assets/stylesheets/activeadmin_addons/inputs/date-time-picker.scss +5 -0
  123. data/app/assets/stylesheets/activeadmin_addons/inputs/numeric-range-filter.scss +12 -0
  124. data/app/assets/stylesheets/activeadmin_addons/inputs/select2.scss +3 -0
  125. data/app/assets/stylesheets/activeadmin_addons/inputs/selected-list.scss +27 -0
  126. data/app/assets/stylesheets/activeadmin_addons/material.scss +53 -0
  127. data/app/assets/stylesheets/activeadmin_addons/vendor/palette-color-picker.scss +155 -0
  128. data/app/inputs/active_admin/inputs/select_input.rb +11 -0
  129. data/app/inputs/ajax_filter_input.rb +6 -0
  130. data/app/inputs/color_picker_input.rb +37 -0
  131. data/app/inputs/date_range_input.rb +5 -0
  132. data/app/inputs/date_time_picker_input.rb +64 -0
  133. data/app/inputs/nested_level_input.rb +51 -0
  134. data/app/inputs/nested_select_input.rb +88 -0
  135. data/app/inputs/numeric_range_filter_input.rb +27 -0
  136. data/app/inputs/range_select_input.rb +6 -0
  137. data/app/inputs/search_select_filter_input.rb +12 -0
  138. data/app/inputs/search_select_input.rb +29 -0
  139. data/app/inputs/selected_list_input.rb +54 -0
  140. data/app/inputs/tags_input.rb +55 -0
  141. data/app/javascript/activeadmin_addons/addons/interactive_select_tag.js +95 -0
  142. data/app/javascript/activeadmin_addons/addons/toggle_bool.js +39 -0
  143. data/app/javascript/activeadmin_addons/all.js +14 -0
  144. data/app/javascript/activeadmin_addons/config.js +10 -0
  145. data/app/javascript/activeadmin_addons/inputs/color-picker.js +16 -0
  146. data/app/javascript/activeadmin_addons/inputs/date-time-picker.js +28 -0
  147. data/app/javascript/activeadmin_addons/inputs/nested-select.js +170 -0
  148. data/app/javascript/activeadmin_addons/inputs/search-select.js +76 -0
  149. data/app/javascript/activeadmin_addons/inputs/select2.js +52 -0
  150. data/app/javascript/activeadmin_addons/inputs/selected-list.js +107 -0
  151. data/app/javascript/activeadmin_addons/inputs/tags.js +76 -0
  152. data/app/javascript/activeadmin_addons/vendor/jquery_palette_color_picker/palette-color-picker.js +265 -0
  153. data/lib/activeadmin_addons.rb +27 -0
  154. data/lib/activeadmin_addons/active_admin_config.rb +9 -0
  155. data/lib/activeadmin_addons/addons/attachment_builder.rb +60 -0
  156. data/lib/activeadmin_addons/addons/bool_builder.rb +31 -0
  157. data/lib/activeadmin_addons/addons/image_builder.rb +12 -0
  158. data/lib/activeadmin_addons/addons/list_builder.rb +73 -0
  159. data/lib/activeadmin_addons/addons/number_builder.rb +25 -0
  160. data/lib/activeadmin_addons/addons/state_builder.rb +39 -0
  161. data/lib/activeadmin_addons/addons/tag_builder.rb +91 -0
  162. data/lib/activeadmin_addons/addons/toggle_bool_builder.rb +44 -0
  163. data/lib/activeadmin_addons/engine.rb +19 -0
  164. data/lib/activeadmin_addons/support/custom_builder.rb +91 -0
  165. data/lib/activeadmin_addons/support/enumerize_formtastic_support.rb +18 -0
  166. data/lib/activeadmin_addons/support/input_base.rb +53 -0
  167. data/lib/activeadmin_addons/support/input_helpers/filter_input.rb +8 -0
  168. data/lib/activeadmin_addons/support/input_helpers/filter_input_methods.rb +32 -0
  169. data/lib/activeadmin_addons/support/input_helpers/input_html_helpers.rb +43 -0
  170. data/lib/activeadmin_addons/support/input_helpers/input_methods.rb +67 -0
  171. data/lib/activeadmin_addons/support/input_helpers/input_options_handler.rb +90 -0
  172. data/lib/activeadmin_addons/support/input_helpers/select_helpers.rb +89 -0
  173. data/lib/activeadmin_addons/support/set_datepicker.rb +13 -0
  174. data/lib/activeadmin_addons/version.rb +3 -0
  175. data/lib/activeadmin_addons_rails_6.rb +5 -0
  176. data/lib/generators/activeadmin_addons/assets/assets_generator.rb +53 -0
  177. data/lib/generators/activeadmin_addons/install/install_generator.rb +19 -0
  178. data/lib/generators/activeadmin_addons/install/templates/initializer.rb +12 -0
  179. data/lib/generators/activeadmin_addons/webpacker/webpacker_generator.rb +35 -0
  180. data/lib/tasks/activeadmin_addons_tasks.rake +4 -0
  181. data/vendor/assets/select2/select2.css +481 -0
  182. data/vendor/assets/select2/select2.full.js +6820 -0
  183. metadata +544 -0
@@ -0,0 +1,91 @@
1
+ module ActiveAdminAddons
2
+ class CustomBuilder
3
+ attr_accessor :context, :model, :args, :block
4
+
5
+ def initialize(context, model, *args, &block)
6
+ @context = context
7
+ @model = model
8
+ @args = *args
9
+ @block = block
10
+ end
11
+
12
+ def render
13
+ raise NotImplementedError
14
+ end
15
+
16
+ def self.create_view_methods
17
+ builder_class = self
18
+ builder_name = builder_method_name
19
+
20
+ ::ActiveAdmin::Views::TableFor.class_eval do
21
+ define_method("#{builder_name}_column") do |*args, &block|
22
+ column(*args) { |model| builder_class.new(self, model, *args, &block).render }
23
+ end
24
+ end
25
+
26
+ ::ActiveAdmin::Views::AttributesTable.class_eval do
27
+ define_method("#{builder_name}_row") do |*args, &block|
28
+ row(*args) { |model| builder_class.new(self, model, *args, &block).render }
29
+ end
30
+ end
31
+ end
32
+
33
+ def self.builder_method_name
34
+ name.underscore.to_s.split("/").last.chomp("_builder")
35
+ end
36
+
37
+ protected
38
+
39
+ def resource_url
40
+ admin_resource&.route_instance_path(model, {})
41
+ end
42
+
43
+ def enabled_controller_action?(action)
44
+ admin_controller_actions.include?(action)
45
+ end
46
+
47
+ def admin_controller_actions
48
+ (admin_controller&.action_methods&.to_a || []).map(&:to_sym)
49
+ end
50
+
51
+ def admin_controller
52
+ context&.controller
53
+ end
54
+
55
+ def admin_resource
56
+ context.active_admin_resource_for(model.class)
57
+ end
58
+
59
+ def data
60
+ @data ||= block ? block.call(model) : model.send(attribute)
61
+ end
62
+
63
+ def options
64
+ @options ||= has_opts? ? args.last : {}
65
+ end
66
+
67
+ def class_name
68
+ model.model_name.param_key
69
+ end
70
+
71
+ # attachment_column :foto
72
+ # attachment_column :foto, style: :hola
73
+ # attachment_column "Mi Foto", :foto
74
+ # attachment_column "Mi Foto", :foto, {}
75
+ # attachment_column :foto do
76
+ # end
77
+ def attribute
78
+ @attribute ||= has_label? ? args[1] : args[0]
79
+ end
80
+
81
+ def has_label?
82
+ has_opts? ? args.length == 3 : args.length == 2
83
+ end
84
+
85
+ def has_opts?
86
+ @has_opts ||= args.last.is_a?(Hash)
87
+ end
88
+
89
+ create_view_methods
90
+ end
91
+ end
@@ -0,0 +1,18 @@
1
+ module ActiveAdminAddons
2
+ module RansackFormBuilderExtension
3
+ def input(method, options = {})
4
+ if object.is_a?(::Ransack::Search)
5
+ klass = object.klass
6
+
7
+ if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[method])
8
+ options[:collection] ||= attr.options
9
+ options[:as] = :select
10
+ end
11
+ end
12
+
13
+ super(method, options)
14
+ end
15
+ end
16
+ end
17
+
18
+ ::Formtastic::FormBuilder.send :prepend, ActiveAdminAddons::RansackFormBuilderExtension
@@ -0,0 +1,53 @@
1
+ require_relative "input_helpers/input_options_handler"
2
+ require_relative "input_helpers/input_methods"
3
+ require_relative "input_helpers/input_html_helpers"
4
+
5
+ module ActiveAdminAddons
6
+ class InputBase < Formtastic::Inputs::StringInput
7
+ include InputOptionsHandler
8
+ include InputMethods
9
+ include InputHtmlHelpers
10
+
11
+ def to_html
12
+ load_input_class
13
+ load_control_attributes
14
+ render_custom_input
15
+ if parts.any?
16
+ return input_wrapping { parts_to_html }
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ def input_html_options
23
+ # maxwidth and size are added by Formtastic::Inputs::StringInput
24
+ # but according to the HTML standard these are not valid attributes
25
+ # on the inputs provided by this module
26
+ super.except(:maxlength, :size).merge(control_attributes)
27
+ end
28
+
29
+ def parts_to_html
30
+ parts.flatten.join("\n").html_safe
31
+ end
32
+
33
+ def load_input_class
34
+ load_class(self.class.to_s.underscore.dasherize)
35
+ end
36
+
37
+ def load_control_attributes
38
+ # Override on child classes if needed
39
+ end
40
+
41
+ def render_custom_input
42
+ # Override on child classes if needed
43
+ end
44
+
45
+ def parts
46
+ @parts ||= []
47
+ end
48
+
49
+ def concat(part)
50
+ parts << part
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,8 @@
1
+ require_relative "filter_input_methods"
2
+
3
+ module ActiveAdminAddons
4
+ module FilterInput
5
+ include ActiveAdmin::Inputs::Filters::Base
6
+ include FilterInputMethods
7
+ end
8
+ end
@@ -0,0 +1,32 @@
1
+ require_relative "input_methods"
2
+
3
+ module ActiveAdminAddons
4
+ module FilterInputMethods
5
+ include InputMethods
6
+
7
+ def object_class
8
+ valid_object.klass
9
+ end
10
+
11
+ def input_value
12
+ result = valid_object.conditions.find do |condition|
13
+ condition.attributes.map(&:name).include?(valid_method.to_s)
14
+ end
15
+
16
+ return unless result
17
+ result.values.first.value
18
+ end
19
+
20
+ def eq_input_name
21
+ "#{valid_method}_eq"
22
+ end
23
+
24
+ def gteq_input_name
25
+ "#{method}_gteq"
26
+ end
27
+
28
+ def lteq_input_name
29
+ "#{method}_lteq"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,43 @@
1
+ module ActiveAdminAddons
2
+ module InputHtmlHelpers
3
+ include InputMethods
4
+
5
+ def prefixed_method
6
+ "#{model_name}_#{valid_method}"
7
+ end
8
+
9
+ def method_to_input_name
10
+ "#{model_name}[#{valid_method}]"
11
+ end
12
+
13
+ def method_to_input_array_name
14
+ "#{method_to_input_name}[]"
15
+ end
16
+
17
+ def method_to_input_id(id)
18
+ raise "invalid input id" if id.blank?
19
+ "#{prefixed_method}_#{id}"
20
+ end
21
+
22
+ def selected_values_id
23
+ "#{prefixed_method}_selected_values"
24
+ end
25
+
26
+ def empty_input_id
27
+ "#{prefixed_method}_empty"
28
+ end
29
+
30
+ def build_separator
31
+ template.content_tag(:span, "-", class: "separator")
32
+ end
33
+
34
+ def build_hidden_control(id, name, value = nil)
35
+ builder.hidden_field(
36
+ valid_method,
37
+ id: id,
38
+ name: name,
39
+ value: value
40
+ )
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,67 @@
1
+ module ActiveAdminAddons
2
+ module InputMethods
3
+ def model_name
4
+ valid_object.class.to_s.underscore.tr('/', '_')
5
+ end
6
+
7
+ def valid_method
8
+ raise "invalid method given" if method.blank?
9
+ method
10
+ end
11
+
12
+ def valid_object
13
+ raise "blank object given" if @object.blank?
14
+ @object
15
+ end
16
+
17
+ def object_class
18
+ valid_object.class
19
+ end
20
+
21
+ def association_name
22
+ valid_method.to_s.singularize.chomp("_id")
23
+ end
24
+
25
+ def method_model
26
+ @options[:method_model] ||
27
+ object_class.reflect_on_association(association_name).try(:klass) ||
28
+ association_name.classify.constantize
29
+ end
30
+
31
+ def tableize_method
32
+ association_name.tableize
33
+ end
34
+
35
+ def input_related_items
36
+ valid_object.send(tableize_method)
37
+ rescue NoMethodError
38
+ raise "no association called #{tableize_method} on #{model_name} model"
39
+ end
40
+
41
+ def input_value
42
+ @input_value ||= valid_object.send(valid_method)
43
+ end
44
+
45
+ def translated_method
46
+ valid_object.class.human_attribute_name(valid_method)
47
+ end
48
+
49
+ def url_from_method
50
+ url = ["/"]
51
+
52
+ if ActiveAdmin.application.default_namespace.present?
53
+ url << "#{ActiveAdmin.application.default_namespace}/"
54
+ end
55
+
56
+ url << tableize_method
57
+ url.join("")
58
+ end
59
+
60
+ def build_virtual_attr
61
+ attribute_name = "virtual_#{valid_method}_attr"
62
+ fail "#{attribute_name} is already defined" if valid_object.respond_to?(attribute_name)
63
+ valid_object.singleton_class.send(:attr_accessor, attribute_name)
64
+ attribute_name
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,90 @@
1
+ module ActiveAdminAddons
2
+ module InputOptionsHandler
3
+ def control_attributes
4
+ result = {}
5
+ result[:class] = css_classes.compact.join(" ") if css_classes.any?
6
+
7
+ data_attr_options.each do |attribute, item|
8
+ result[:data] ||= {}
9
+ result[:data][attribute] = formatted_option_value(item)
10
+ end
11
+
12
+ attr_options.each do |attribute, item|
13
+ result[attribute] = formatted_option_value(item)
14
+ end
15
+
16
+ result
17
+ end
18
+
19
+ def data_attr_options
20
+ @data_attr_options || {}
21
+ end
22
+
23
+ def attr_options
24
+ @attr_options || {}
25
+ end
26
+
27
+ def css_classes
28
+ @css_classes || []
29
+ end
30
+
31
+ def load_data_attr(attr_name, options = {})
32
+ @data_attr_options ||= {}
33
+ @data_attr_options[attr_name.to_sym] = load_option_attr(attr_name, options)
34
+ end
35
+
36
+ def load_attr(attr_name, options = {})
37
+ @attr_options ||= {}
38
+ @attr_options[attr_name.to_sym] = load_option_attr(attr_name, options)
39
+ end
40
+
41
+ def load_class(class_attr)
42
+ @css_classes ||= []
43
+ return if class_attr.blank?
44
+ class_attr = class_attr.strip
45
+ @css_classes << class_attr
46
+ class_attr
47
+ end
48
+
49
+ def get_data_attr_value(attr_name)
50
+ item = data_attr_options[attr_name.to_sym]
51
+ return unless item
52
+ item[:value]
53
+ end
54
+
55
+ private
56
+
57
+ def formatted_option_value(item)
58
+ return item[:value].send(item[:formatter]) if item[:formatter]
59
+ item[:value]
60
+ end
61
+
62
+ def load_option_attr(attr_name, options)
63
+ value = calculate_attr_value(attr_name, options)
64
+ item = { value: value }
65
+ item[:formatter] = options[:formatter] unless options[:formatter].blank?
66
+ item
67
+ end
68
+
69
+ def calculate_attr_value(attr_name, attr_options)
70
+ value = attr_options[:value] || @options[attr_name]
71
+
72
+ if !empty_value?(value)
73
+ if value.is_a?(Proc)
74
+ return template.instance_eval(&value)
75
+ else
76
+ return value
77
+ end
78
+ end
79
+
80
+ default_value = attr_options[:default]
81
+ return unless default_value
82
+ return default_value.call if default_value.is_a?(Proc)
83
+ default_value
84
+ end
85
+
86
+ def empty_value?(value)
87
+ value.nil? || value == ""
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,89 @@
1
+ module ActiveAdminAddons
2
+ module SelectHelpers
3
+ include InputMethods
4
+ include InputOptionsHandler
5
+
6
+ def array_to_select_options
7
+ selected_values = input_value.is_a?(Array) ? input_value : input_value.to_s.split(",")
8
+ array = collection.map(&:to_s) + selected_values
9
+ array.sort.map do |value|
10
+ option = { id: value, text: value }
11
+ option[:selected] = "selected" if selected_values.include?(value)
12
+ option
13
+ end.uniq
14
+ end
15
+
16
+ def initial_collection_to_select_options
17
+ initial_options = [[nil]] # add blank option
18
+ selected = selected_item
19
+
20
+ if selected
21
+ selected_option = item_to_select_option(selected)
22
+ initial_options << [selected_option[:text], selected_option[:id]]
23
+ end
24
+
25
+ initial_options
26
+ end
27
+
28
+ def collection_to_select_options
29
+ complete_collection = collection + selected_collection
30
+ complete_collection.map do |item|
31
+ option = item_to_select_option(item)
32
+ yield(item, option) if block_given?
33
+
34
+ if selected_collection.include?(item)
35
+ load_data_attr(:selected, value: option.dup, formatter: :to_json)
36
+ option[:selected] = "selected"
37
+ end
38
+
39
+ option
40
+ end.uniq
41
+ end
42
+
43
+ def item_to_select_option(item)
44
+ return unless item
45
+ {
46
+ id: item.send((valid_options[:value] || :id)),
47
+ text: item.send((valid_options[:display_name] || :name))
48
+ }
49
+ end
50
+
51
+ def active_record_select?
52
+ active_record_relation?(collection) &&
53
+ active_record_relation?(selected_collection)
54
+ rescue NameError
55
+ false
56
+ end
57
+
58
+ def selected_collection
59
+ @selected_collection ||= begin
60
+ if active_record_relation?(collection)
61
+ collection.model.where(id: input_value)
62
+ else
63
+ method_model.where(id: input_value)
64
+ end
65
+ end
66
+ end
67
+
68
+ def selected_item
69
+ @selected_item ||= selected_collection.first
70
+ end
71
+
72
+ private
73
+
74
+ def active_record_relation?(value)
75
+ klass = value.class.name
76
+ klass == "ActiveRecord::Relation" ||
77
+ klass == "ActiveRecord::Associations::CollectionProxy"
78
+ end
79
+
80
+ def valid_options
81
+ raise "missing @options hash" unless !!@options
82
+ @options
83
+ end
84
+
85
+ def collection
86
+ valid_options[:collection] || []
87
+ end
88
+ end
89
+ end