active_admin_simple_life 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a2dbffd025fcc68736c506d897440a46c2267c0
4
+ data.tar.gz: 9ebceb079da704c2ef06c17e859a9fd8f95f3ae9
5
+ SHA512:
6
+ metadata.gz: 5bde864d944472b403a1c6e88d6d83c8c15e3820bd447b4f10ea6e342a30349cae4c22c81c567d66bd5fdbc8082a023daaa6abeb88c265a5b78036baf8b67857
7
+ data.tar.gz: 4fd154aebd08ccbfafa57bb6f9ca086160036d0065c0909d4af113966ac4075f25224452961801be7276b97ec6626db72bb5ebd1d7175a9747bf28c1ed7c9588
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Kvokka
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.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ #ActiveAdminSimpleLife
2
+
3
+ About
4
+
5
+ Gem automatize routine with creation simple menus in ActiveAdmin with minimum
6
+ code duplication.
7
+
8
+ ###Installation
9
+
10
+ put in `Gemfile`
11
+
12
+ `gem 'active_admin_simple_life'`
13
+
14
+ in `config/initializers/active_admin.rb` change header like this:
15
+
16
+ ```
17
+ # frozen_string_literal: true
18
+ include ActiveAdminSimpleLife
19
+ ActiveAdmin.setup do |config|
20
+ include ActiveAdminSimpleLife::SimpleElements
21
+ # your settings
22
+ ```
23
+
24
+ I used 2 includes, be course `simple_menu_for` method must be in main namespace,
25
+ so it is not a bug ;)
26
+
27
+ ###Usage
28
+
29
+ For all gem methods you need to present class method(NOT scope!) `main fields`,
30
+ where you provide columns, which you would like to manage. Example:
31
+
32
+ ```
33
+ class Foo < ActiveRecord::Base
34
+ ...
35
+ def self.mail_fields
36
+ [:title, :price, :sale_date]
37
+ end
38
+ end
39
+ ```
40
+
41
+ ###Methods
42
+
43
+ `simple_menu_for KlassName, [options]` which will make all dirty work, and just
44
+ jive you simple menu, with only `main_fields` and `filters` in 1 line.
45
+ method takes options like:
46
+ * `priority` = `ActiveAdmin` proxy menu `priority`
47
+ * `parent` = `ActiveAdmin` proxy menu `parent`
48
+ * `[permitted_params] = addition for strong params (by default provides only
49
+ `main_fields`)
50
+
51
+ Parts of `simple_menu_for` may be used for other purposes with:
52
+ * `index_for_main_fields klass`
53
+ * `filter_for_main_fields klass`
54
+ * `form_for_main_fields klass`
55
+
56
+ Other feature is simple menu with 1 single nested klass. For it I made
57
+ * `nested_form_for_main_fields klass, nested_klass`
58
+
59
+ ###I18n
60
+
61
+ For boolean data only
62
+
63
+ ```
64
+ en:
65
+ boolean:
66
+ active: 'Active'
67
+ not_active: 'Not active'
68
+ ```
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ begin
3
+ require "bundler/setup"
4
+ rescue LoadError
5
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
6
+ end
7
+
8
+ require "rdoc/task"
9
+
10
+ RDoc::Task.new(:rdoc) do |rdoc|
11
+ rdoc.rdoc_dir = "rdoc"
12
+ rdoc.title = "ActiveAdminSimpleLife"
13
+ rdoc.options << "--line-numbers"
14
+ rdoc.rdoc_files.include("README.rdoc")
15
+ rdoc.rdoc_files.include("lib/**/*.rb")
16
+ end
17
+
18
+ Bundler::GemHelper.install_tasks
19
+
20
+ require "rake/testtask"
21
+
22
+ Rake::TestTask.new(:test) do |t|
23
+ t.libs << "lib"
24
+ t.libs << "test"
25
+ t.pattern = "test/**/*_test.rb"
26
+ t.verbose = false
27
+ end
28
+
29
+ task default: :test
@@ -0,0 +1,4 @@
1
+ en:
2
+ boolean:
3
+ active: 'Active'
4
+ not_active: 'Not active'
@@ -0,0 +1,4 @@
1
+ ru:
2
+ boolean:
3
+ active: 'Да'
4
+ not_active: 'Нет'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "active_admin_simple_life/engine"
3
+ require "active_admin_simple_life/simple_menu"
4
+ require "active_admin_simple_life/menu_elements"
5
+ module ActiveAdminSimpleLife
6
+ include SimpleMenu
7
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module ActiveAdminSimpleLife
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace ActiveAdminSimpleLife
5
+ end
6
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+ module ActiveAdminSimpleLife
3
+ module SimpleElements
4
+ def index_for_main_fields(klass)
5
+ index download_links: false do
6
+ selectable_column
7
+ id_column
8
+ klass.main_fields.each do |symbol|
9
+ column(I18n.t("activerecord.attributes.#{klass.to_s.underscore}.#{symbol}"), sortable: symbol) do |current|
10
+ field_value = current.send(symbol.cut_id)
11
+ case field_value
12
+ when String
13
+ truncate_field field_value
14
+ when ::ActiveSupport::TimeWithZone, Time, Date
15
+ I18n.l field_value, format: :long
16
+ when TrueClass
17
+ span_true
18
+ when FalseClass
19
+ span_false
20
+ else
21
+ link_to truncate_field(field_value), send(fetch_path(field_value), field_value.id)
22
+ end
23
+ end
24
+ end
25
+ actions
26
+ end
27
+ end
28
+
29
+ def filter_for_main_fields(klass)
30
+ klass.main_fields.each { |f| filter f.cut_id }
31
+ end
32
+
33
+ def form_for_main_fields(klass, &block)
34
+ form do |f|
35
+ f.semantic_errors(*f.object.errors.keys)
36
+ f.inputs do
37
+ klass.main_fields.each { |ff| f.input ff.cut_id }
38
+ f.instance_eval(&block) if block_given?
39
+ end
40
+ f.actions
41
+ end
42
+ end
43
+
44
+ # simple nested set for 2 classes with defined main_fields
45
+ def nested_form_for_main_fields(klass, nested_klass)
46
+ form_for_main_fields(klass) do |form_field|
47
+ nested_table_name = nested_klass.to_s.underscore.pluralize.to_sym
48
+ main_model_name = klass.to_s.underscore.to_sym
49
+ form_field.has_many nested_table_name, allow_destroy: true do |form|
50
+ nested_klass.main_fields.map(&:cut_id).each do |nested_field|
51
+ form.input(nested_field) unless nested_field == main_model_name
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ def cut_id
58
+ to_s.sub(/_id$/, "").to_sym
59
+ end
60
+
61
+ private
62
+
63
+ # TODO: move length to var
64
+ def truncate_field(field)
65
+ truncate(field.to_s, length: 50)
66
+ end
67
+
68
+ def fetch_path(field)
69
+ "admin_#{field.class.to_s.underscore}_path"
70
+ end
71
+
72
+ def span_true
73
+ Arbre::Context.new { span(class: "status_tag yes") { I18n.t("boolean.active") } }
74
+ end
75
+
76
+ def span_false
77
+ Arbre::Context.new { span(class: "status_tag no") { I18n.t "boolean.disactive" } }
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+ module ActiveAdminSimpleLife
3
+ module SimpleMenu
4
+ # for proper work, model must have class method `mail_fields`, which return array of field symbols.
5
+ # references write as is, like `foo_id`
6
+ #
7
+ # in options can take:
8
+ # menu_priority:integer
9
+ # menu_parent:string
10
+ # permitted_params:array for some additions to main_fields permitted params
11
+
12
+ def simple_menu_for(klass, options = {})
13
+ ActiveAdmin.register klass do
14
+ menu_options = options.slice(:priority, :parent)
15
+ menu menu_options if menu_options.any?
16
+ permitted_params = options[:permitted_params]
17
+ permit_params(*(klass.main_fields + (permitted_params || [])))
18
+ actions :all, except: [:show]
19
+
20
+ controller.class_variable_set(:@@permitted_params, permitted_params)
21
+ controller.class_variable_set(:@@klass, klass)
22
+
23
+ controller do
24
+ def scoped_collection
25
+ permitted_params = self.class.class_variable_get :@@permitted_params
26
+ self.class.class_variable_get(:@@klass).includes(*permitted_params.map(&:cut_id))
27
+ end
28
+ end if permitted_params
29
+
30
+ index_for_main_fields klass
31
+ filter_for_main_fields klass
32
+ form_for_main_fields klass
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module ActiveAdminSimpleLife
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # desc "Explaining what the task does"
3
+ # task :active_admin_simple_life do
4
+ # # Task goes here
5
+ # end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_admin_simple_life
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kvokka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activeadmin
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This gem provide the ability to create ActiveAdmin resourceswith 1 line
42
+ and avoid code duplication.
43
+ email:
44
+ - root_p@mail.ru
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - config/locales/en.yml
53
+ - config/locales/ru.yml
54
+ - lib/active_admin_simple_life.rb
55
+ - lib/active_admin_simple_life/engine.rb
56
+ - lib/active_admin_simple_life/menu_elements.rb
57
+ - lib/active_admin_simple_life/simple_menu.rb
58
+ - lib/active_admin_simple_life/version.rb
59
+ - lib/tasks/active_admin_simple_life_tasks.rake
60
+ homepage: https://github.com/kvokka/active_admin_simple_life
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.5.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Quick simple resources creation in ActiveAdmin
84
+ test_files: []
85
+ has_rdoc: