activeadmin_simple_form 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2dde4b5ffb8dac69751a5d2324cdb95cea413c9
4
+ data.tar.gz: 3c8df402522a9b65e7f063a3be1c7070a1fd4314
5
+ SHA512:
6
+ metadata.gz: f97aeac3935ebec92142ea3f5723dd49187197f44624773e220d64c539cbaf5f40e76069b3fa7bfed046fd5fa344311f2566ebddaaf58d31e2e57b73a28740e7
7
+ data.tar.gz: 838312860295bf454d115d15a44bc2f2cc99fa876c854f763ac3c8d447eb57b5872132103762ec51a58534675a5a5ec766bb09aa2e041a5353571c29b5c4651b
@@ -0,0 +1,3 @@
1
+ _misc/
2
+ Gemfile.lock
3
+ *.orig
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2018 Mattia Roccoberton
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # ActiveAdmin SimpleForm [![Gem Version](https://badge.fury.io/rb/activeadmin_simple_form.svg)](https://badge.fury.io/rb/activeadmin_simple_form)
2
+
3
+ A plugin to use Simple Form in place of Formtastic in Active Admin edit views.
4
+
5
+ WARNING: this component is a Beta version, some Active Admin functionalities could not work as expected
6
+
7
+ ## Install
8
+
9
+ - Add to your Gemfile: gem 'activeadmin_simple_form'
10
+ - Execute bundle
11
+ - Add to Simple Form config initializer (wrapper used inside *inputs* blocks):
12
+
13
+ ```rb
14
+ config.wrappers :inputs_container, tag: :li, class: :input, hint_class: :field_with_hint, error_class: :field_with_errors do |b|
15
+ b.use :html5
16
+ b.use :placeholder
17
+ b.optional :maxlength
18
+ b.optional :minlength
19
+ b.optional :pattern
20
+ b.optional :min_max
21
+ b.optional :readonly
22
+ b.use :label_input
23
+ b.use :hint, wrap_with: { tag: :span, class: :hint }
24
+ b.use :error, wrap_with: { tag: :span, class: :error }
25
+ end
26
+ ```
27
+
28
+ ## Example
29
+
30
+ - Author model example:
31
+
32
+ ```rb
33
+ form do |f|
34
+ f.inputs 'Informations' do
35
+ f.input :name
36
+ f.input :age
37
+ f.association :country # using input for associations is not supported
38
+ f.has_many :articles do |ff|
39
+ ff.input :title
40
+ ff.input :description
41
+ ff.input :published
42
+ ff.input :_destroy, as: :boolean, label: 'Remove', required: false unless ff.object.new_record?
43
+ end
44
+ end
45
+ f.actions
46
+ end
47
+ ```
48
+
49
+ ## Do you like it? Star it!
50
+
51
+ If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
52
+
53
+ Take a look at [other ActiveAdmin components](https://github.com/blocknotes?utf8=✓&tab=repositories&q=activeadmin&type=source) that I made if you are curious.
54
+
55
+ ## Contributors
56
+
57
+ - [Mattia Roccoberton](http://blocknot.es) - creator, maintainer
58
+
59
+ ## License
60
+
61
+ [MIT](LICENSE.txt)
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'activeadmin/simple_form/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activeadmin_simple_form'
7
+ spec.version = ActiveAdmin::SimpleForm::VERSION
8
+ spec.summary = 'simple_form for ActiveAdmin'
9
+ spec.description = 'An Active Admin plugin to use Simple Form in place of Formtastic in edit views'
10
+ spec.license = 'MIT'
11
+ spec.authors = ['Mattia Roccoberton']
12
+ spec.email = 'mat@blocknot.es'
13
+ spec.homepage = 'https://github.com/blocknotes/activeadmin_simple_form'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_runtime_dependency 'activeadmin', '~> 1.0'
19
+ spec.add_runtime_dependency 'simple_form', '~> 3.5'
20
+ end
@@ -0,0 +1,2 @@
1
+ require 'simple_form'
2
+ require 'activeadmin/simple_form/engine'
@@ -0,0 +1,201 @@
1
+ require 'active_admin'
2
+
3
+ module ActiveAdmin
4
+ module SimpleForm
5
+ class Engine < ::Rails::Engine
6
+ engine_name 'activeadmin_simple_form'
7
+ end
8
+ end
9
+ end
10
+
11
+ module ActiveAdmin
12
+ module SimpleForm
13
+ class SimpleFormBuilder < ::SimpleForm::FormBuilder
14
+ include MethodOrProcHelper
15
+ include ::Formtastic::Helpers::InputsHelper
16
+
17
+ attr_accessor :already_in_an_inputs_block
18
+
19
+ # def initialize(*)
20
+ # raise Exception.new( 'define a "inputs_container" simple_form wrapper' ) unless ::SimpleForm.wrappers['inputs_container']
21
+ # ::SimpleForm.wrappers['inputs_container'] ||= ::SimpleForm::Wrappers::Root.new [], tag: :li
22
+ # super
23
+ # end
24
+
25
+ def action(method, options = {}) # -> Formtastic::Helpers::ActionHelper
26
+ case method
27
+ when :button, :reset
28
+ button :button, options[:label], type: method
29
+ when :submit
30
+ button :submit, options[:label]
31
+ else
32
+ template.link_to options[:label], options[:url]
33
+ end
34
+ end
35
+
36
+ def assoc_heading(assoc) # -> ActiveAdmin::FormBuilder
37
+ object.class.reflect_on_association(assoc).klass.model_name.
38
+ human(count: ::ActiveAdmin::Helpers::I18n::PLURAL_MANY_COUNT)
39
+ end
40
+
41
+ def cancel_link(url = {action: "index"}, html_options = {}, li_attrs = {}) # -> ActiveAdmin::FormBuilder
42
+ li_attrs[:class] ||= "cancel"
43
+ li_content = template.link_to ::I18n.t('active_admin.cancel'), url, html_options
44
+ template.content_tag(:li, li_content, li_attrs)
45
+ end
46
+
47
+ def has_many(assoc, options = {}, &block) # -> ActiveAdmin::FormBuilder
48
+ # remove options that should not render as attributes
49
+ custom_settings = :new_record, :allow_destroy, :heading, :sortable, :sortable_start
50
+ builder_options = {new_record: true}.merge! options.slice *custom_settings
51
+ options = {for: assoc }.merge! options.except *custom_settings
52
+ options[:class] = [options[:class], "inputs has_many_fields"].compact.join(' ')
53
+ sortable_column = builder_options[:sortable]
54
+ sortable_start = builder_options.fetch(:sortable_start, 0)
55
+
56
+ if sortable_column
57
+ options[:for] = [assoc, sorted_children(assoc, sortable_column)]
58
+ end
59
+
60
+ html = "".html_safe
61
+ unless builder_options.key?(:heading) && !builder_options[:heading]
62
+ html << template.content_tag(:h3) do
63
+ builder_options[:heading] || assoc_heading(assoc)
64
+ end
65
+ end
66
+
67
+ html << template.capture do
68
+ form_block = proc do |has_many_form|
69
+ index = parent_child_index options[:parent] if options[:parent]
70
+ block_contents = template.capture do
71
+ block.call(has_many_form, index)
72
+ end
73
+ template.concat(block_contents)
74
+ template.concat has_many_actions(has_many_form, builder_options, "".html_safe)
75
+ end
76
+
77
+ template.assigns[:has_many_block] = true
78
+ contents = without_wrapper { inputs(options, &form_block) } || "".html_safe
79
+
80
+ if builder_options[:new_record]
81
+ contents << js_for_has_many(assoc, form_block, template, builder_options[:new_record], options[:class])
82
+ else
83
+ contents
84
+ end
85
+ end
86
+
87
+ tag = @already_in_an_inputs_block ? :li : :div
88
+ html = template.content_tag(tag, html, class: "has_many_container #{assoc}", 'data-sortable' => sortable_column, 'data-sortable-start' => sortable_start)
89
+ template.concat(html) if template.output_buffer
90
+ html
91
+ end
92
+
93
+ def input(attribute_name, options = {}, &block) # -> SimpleForm::FormBuilder
94
+ options = @defaults ? @defaults.deep_dup.deep_merge(options) : options.dup
95
+ options[:wrapper] = :inputs_container if @already_in_an_inputs_block && !options[:wrapper] && ::SimpleForm.wrappers['inputs_container']
96
+ options[:wrapper_html] ||= {}
97
+
98
+ input = find_input(attribute_name, options, &block)
99
+ unless options[:wrapper_html][:class]
100
+ case input.input_type
101
+ when :date, :datetime, :time
102
+ options[:wrapper_html][:class] = 'fragment'
103
+ end
104
+ end
105
+ wrapper = find_wrapper(input.input_type, options)
106
+
107
+ html = wrapper.render input
108
+ template.concat(html) if template.output_buffer && template.assigns[:has_many_block]
109
+ html
110
+ end
111
+
112
+ # def object
113
+ # # template.resource
114
+ # form_builder.object
115
+ # end
116
+
117
+ protected
118
+
119
+ def has_many_actions(has_many_form, builder_options, contents) # -> ActiveAdmin::FormBuilder
120
+ if has_many_form.object.new_record?
121
+ contents << template.content_tag(:li) do
122
+ template.link_to I18n.t('active_admin.has_many_remove'), "#", class: 'button has_many_remove'
123
+ end
124
+ elsif call_method_or_proc_on(has_many_form.object,
125
+ builder_options[:allow_destroy],
126
+ exec: false)
127
+
128
+ has_many_form.input(:_destroy, as: :boolean,
129
+ wrapper_html: {class: 'has_many_delete'},
130
+ label: I18n.t('active_admin.has_many_delete'))
131
+ end
132
+
133
+ if builder_options[:sortable]
134
+ has_many_form.input builder_options[:sortable], as: :hidden
135
+
136
+ contents << template.content_tag(:li, class: 'handle') do
137
+ I18n.t('active_admin.move')
138
+ end
139
+ end
140
+
141
+ contents
142
+ end
143
+
144
+ def sorted_children(assoc, column) # -> ActiveAdmin::FormBuilder
145
+ object.public_send(assoc).sort_by do |o|
146
+ attribute = o.public_send column
147
+ [attribute.nil? ? Float::INFINITY : attribute, o.id || Float::INFINITY]
148
+ end
149
+ end
150
+
151
+ private
152
+
153
+ def js_for_has_many(assoc, form_block, template, new_record, class_string) # -> ActiveAdmin::FormBuilder
154
+ assoc_reflection = object.class.reflect_on_association assoc
155
+ assoc_name = assoc_reflection.klass.model_name
156
+ placeholder = "NEW_#{assoc_name.to_s.underscore.upcase.gsub(/\//, '_')}_RECORD"
157
+ opts = {
158
+ for: [assoc, assoc_reflection.klass.new],
159
+ class: class_string,
160
+ for_options: { child_index: placeholder }
161
+ }
162
+ html = template.capture{ inputs_for_nested_attributes opts, &form_block }
163
+ text = new_record.is_a?(String) ? new_record : I18n.t('active_admin.has_many_new', model: assoc_name.human)
164
+
165
+ template.link_to text, '#', class: "button has_many_add", data: {
166
+ html: CGI.escapeHTML(html).html_safe, placeholder: placeholder
167
+ }
168
+ end
169
+
170
+ def without_wrapper # -> ActiveAdmin::FormBuilder
171
+ is_being_wrapped = @already_in_an_inputs_block
172
+ @already_in_an_inputs_block = false
173
+
174
+ html = yield
175
+
176
+ @already_in_an_inputs_block = is_being_wrapped
177
+ html
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ ActiveAdmin::Views::ActiveAdminForm.class_eval do
184
+ def build(resource, options = {}, &block) # -> ActiveAdmin::Views::ActiveAdminForm
185
+ @resource = resource
186
+ options = options.deep_dup
187
+ options[:builder] ||= ActiveAdmin::SimpleForm::SimpleFormBuilder # change 1 - was ActiveAdmin::FormBuilder
188
+ form_string = helpers.simple_form_for(resource, options) do |f| # change 2 - was helpers.semantic_form_for
189
+ @form_builder = f
190
+ end
191
+
192
+ @opening_tag, @closing_tag = split_string_on(form_string, "</form>")
193
+ instance_eval(&block) if block_given?
194
+
195
+ # Rails sets multipart automatically if a file field is present,
196
+ # but the form tag has already been rendered before the block eval.
197
+ if multipart? && @opening_tag !~ /multipart/
198
+ @opening_tag.sub!(/<form/, '<form enctype="multipart/form-data"')
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveAdmin
2
+ module SimpleForm
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'activeadmin/simple_form'
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeadmin_simple_form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mattia Roccoberton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeadmin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: simple_form
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.5'
41
+ description: An Active Admin plugin to use Simple Form in place of Formtastic in edit
42
+ views
43
+ email: mat@blocknot.es
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - activeadmin_simple_form.gemspec
54
+ - lib/activeadmin/simple_form.rb
55
+ - lib/activeadmin/simple_form/engine.rb
56
+ - lib/activeadmin/simple_form/version.rb
57
+ - lib/activeadmin_simple_form.rb
58
+ homepage: https://github.com/blocknotes/activeadmin_simple_form
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.14
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: simple_form for ActiveAdmin
82
+ test_files: []