admin_bits 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +20 -0
  3. data/LICENSE +165 -0
  4. data/README.md +19 -0
  5. data/Rakefile +33 -0
  6. data/lib/admin_bits.rb +50 -0
  7. data/lib/admin_bits/admin_resource.rb +96 -0
  8. data/lib/admin_bits/admin_resource/paginator.rb +14 -0
  9. data/lib/admin_bits/admin_resource/path_handler.rb +34 -0
  10. data/lib/admin_bits/base_config.rb +38 -0
  11. data/lib/admin_bits/engine.rb +4 -0
  12. data/lib/admin_bits/helpers.rb +44 -0
  13. data/lib/admin_bits/scopes.rb +52 -0
  14. data/lib/admin_bits/utils.rb +16 -0
  15. data/lib/generators/admin_bits_generator.rb +52 -0
  16. data/lib/generators/templates/controller.rb.erb +35 -0
  17. data/lib/generators/templates/index.html.erb +0 -0
  18. data/lib/generators/templates/layout.html.erb +3 -0
  19. data/test/admin_bits_test.rb +7 -0
  20. data/test/dummy/Rakefile +7 -0
  21. data/test/dummy/app/assets/stylesheets/admin_bits.css +0 -0
  22. data/test/dummy/app/controllers/admin/items_controller.rb +40 -0
  23. data/test/dummy/app/controllers/application_controller.rb +3 -0
  24. data/test/dummy/app/helpers/application_helper.rb +2 -0
  25. data/test/dummy/app/models/basic_admin_panel.rb +65 -0
  26. data/test/dummy/app/models/item.rb +7 -0
  27. data/test/dummy/app/views/admin/items/index.html.erb +30 -0
  28. data/test/dummy/app/views/admin_bits_modules/basic_admin_panel/edit.html.erb +4 -0
  29. data/test/dummy/app/views/admin_bits_modules/basic_admin_panel/index.html.erb +30 -0
  30. data/test/dummy/app/views/layouts/admin_bits_modules/basic_admin_panel/layout.html.erb +13 -0
  31. data/test/dummy/config.ru +4 -0
  32. data/test/dummy/config/application.rb +45 -0
  33. data/test/dummy/config/boot.rb +10 -0
  34. data/test/dummy/config/database.yml +22 -0
  35. data/test/dummy/config/environment.rb +5 -0
  36. data/test/dummy/config/environments/development.rb +27 -0
  37. data/test/dummy/config/environments/production.rb +50 -0
  38. data/test/dummy/config/environments/test.rb +36 -0
  39. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  40. data/test/dummy/config/initializers/inflections.rb +10 -0
  41. data/test/dummy/config/initializers/mime_types.rb +5 -0
  42. data/test/dummy/config/initializers/secret_token.rb +7 -0
  43. data/test/dummy/config/initializers/session_store.rb +8 -0
  44. data/test/dummy/config/locales/en.yml +5 -0
  45. data/test/dummy/config/routes.rb +57 -0
  46. data/test/dummy/db/development.sqlite3 +0 -0
  47. data/test/dummy/db/migrate/20131110141830_create_items.rb +15 -0
  48. data/test/dummy/db/schema.rb +25 -0
  49. data/test/dummy/db/test.sqlite3 +0 -0
  50. data/test/dummy/log/development.log +2368 -0
  51. data/test/dummy/log/test.log +672 -0
  52. data/test/dummy/public/404.html +26 -0
  53. data/test/dummy/public/422.html +26 -0
  54. data/test/dummy/public/500.html +26 -0
  55. data/test/dummy/public/favicon.ico +0 -0
  56. data/test/dummy/public/javascripts/application.js +2 -0
  57. data/test/dummy/public/javascripts/controls.js +965 -0
  58. data/test/dummy/public/javascripts/dragdrop.js +974 -0
  59. data/test/dummy/public/javascripts/effects.js +1123 -0
  60. data/test/dummy/public/javascripts/prototype.js +6001 -0
  61. data/test/dummy/public/javascripts/rails.js +202 -0
  62. data/test/dummy/script/rails +6 -0
  63. data/test/integration/ordering_test.rb +14 -0
  64. data/test/support/integration_case.rb +5 -0
  65. data/test/test_helper.rb +22 -0
  66. data/test/unit/path_handler_test.rb +17 -0
  67. data/test/unit/sorting_test.rb +49 -0
  68. metadata +123 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 726a039e14b8abd697e9263f2cb0a4296400308c
4
+ data.tar.gz: 1b7822e3600d2ad2a47a5c1e398501d0bc2e7939
5
+ SHA512:
6
+ metadata.gz: 11d979132be94cb24ac518b982ce8beed6f1110f827f6c6eee66d2de0079218b2276c009ff2bdb271eb5c610283a5aaf1d7a3f3830021916fd43f1e1f069ceb8
7
+ data.tar.gz: 542a726458c8e228679a8df973acf5a73bfe885a8993dc9d90456b2e9cdcd7b07279c0ba7289e8fa913d9e0e4ed3112e316fba4178a7ce381a50dc6b9fb8e887
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", ">= 4.0.0"
4
+ gem "capybara", ">= 0.4.0"
5
+ gem "sqlite3"
6
+ gem "kaminari"
7
+ gem "simple_form"
8
+
9
+ # group :test, :development do
10
+ # gem 'rspec-rails'
11
+ # end
12
+
13
+ group :test do
14
+ gem "rake"
15
+ gem "mocha"
16
+ end
17
+
18
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
19
+ # gem 'ruby-debug'
20
+ # gem 'ruby-debug19'
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ admin_bits
2
+ ==========
3
+
4
+ [![Build Status](https://travis-ci.org/bitmincer/admin_bits.svg)](https://travis-ci.org/bitmincer/admin_bits)
5
+
6
+ admin_bits is a gem for creating all kinds of dashboards and admin panels.
7
+
8
+ ## Credits
9
+
10
+ admin_bits is maintained and funded by bitmincer. Thank you
11
+ to all the [contributors][contributors].
12
+
13
+ ## License
14
+
15
+ admin_bits is copyright © 2013-2014 bitmincer. It is free software,
16
+ and may be redistributed under the terms specified in the
17
+ [LICENSE](LICENSE) file.
18
+
19
+ [contributors]: https://github.com/bitmincer/admin_bits/contributors
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ Bundler::GemHelper.install_tasks
10
+
11
+ require 'rake'
12
+ require 'rdoc/task'
13
+
14
+ require 'rake/testtask'
15
+
16
+ Rake::TestTask.new(:test) do |t|
17
+ t.libs << 'lib'
18
+ t.libs << 'test'
19
+ t.pattern = 'test/**/*_test.rb'
20
+ t.verbose = false
21
+ end
22
+
23
+ task :default => :test
24
+
25
+ Rake::RDocTask.new(:rdoc) do |rdoc|
26
+ rdoc.rdoc_dir = 'rdoc'
27
+ rdoc.title = 'AdminBits'
28
+ rdoc.options << '--line-numbers' << '--inline-source'
29
+ rdoc.rdoc_files.include('README.rdoc')
30
+ rdoc.rdoc_files.include('lib/**/*.rb')
31
+ end
32
+
33
+
data/lib/admin_bits.rb ADDED
@@ -0,0 +1,50 @@
1
+ module AdminBits
2
+ autoload :BaseConfig, 'admin_bits/base_config'
3
+ autoload :Helpers, 'admin_bits/helpers'
4
+ autoload :AdminResource, 'admin_bits/admin_resource'
5
+ autoload :PathHandler, 'admin_bits/admin_resource/path_handler'
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ def declare_resource(name, options = {}, &block)
13
+ raise "Name must be Symbol" unless name.is_a?(Symbol)
14
+
15
+ ab_config = AdminBits::BaseConfig.new
16
+ ab_config.instance_eval &block
17
+
18
+ helper_method name
19
+ helper Helpers
20
+ helper_method :admin_resource
21
+ helper_method :admin_filter
22
+
23
+ define_method :admin_resource do
24
+ AdminResource.new(
25
+ name,
26
+ raw_resource,
27
+ ab_config,
28
+ action_name,
29
+ params
30
+ )
31
+ end
32
+
33
+ define_method :raw_resource do
34
+ instance_variable_get("@#{name}")
35
+ end
36
+
37
+ define_method :admin_filter do |name|
38
+ admin_resource.filter_params[name]
39
+ end
40
+
41
+ define_method name do
42
+ admin_resource.output
43
+ end
44
+
45
+ if mods = ab_config.get_mods
46
+ mods.new(self)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,96 @@
1
+ module AdminBits
2
+ class AdminResource
3
+ attr_reader :options, :resource, :request_params, :action_name, :name
4
+
5
+ def initialize(name, resource, options, action_name, request_params = {})
6
+ @resource = resource
7
+ @options = options
8
+ @request_params = request_params
9
+ @action_name = action_name
10
+ @name = name
11
+
12
+ raise ":path must be provided" unless @options[:path]
13
+
14
+ sanitize_params
15
+ end
16
+
17
+ def sanitize_params
18
+ @request_params = request_params.
19
+ with_indifferent_access.
20
+ reject do |k,v|
21
+ ["action", "controller", "commit"].include?(k) || v.blank?
22
+ end
23
+
24
+ request_params[:order] ||= default_order
25
+ request_params[:asc] ||= default_asc
26
+ end
27
+
28
+ def default_order
29
+ options[:default_order].to_s
30
+ end
31
+
32
+ def default_asc
33
+ options[:default_direction] == :asc ? "true" : "false"
34
+ end
35
+
36
+ def filter_params
37
+ (request_params[:filters] || {}).with_indifferent_access
38
+ end
39
+
40
+ def filtered_resource
41
+ return_scope = resource
42
+
43
+ (options[:filters] || {}).each_pair do |scope_name, args|
44
+ if args.is_a?(Array)
45
+ args = args.map {|a| filter_params[a] }
46
+
47
+ return_scope = return_scope.send(scope_name, *args)
48
+ else
49
+ return_scope = return_scope.instance_exec filter_params, &args
50
+ end
51
+ end
52
+
53
+ return_scope
54
+ end
55
+
56
+ def output
57
+ # Paginator.new(filtered_resource.order(get_order), get_page).call
58
+ filtered_resource.order(get_order).page(get_page)
59
+ end
60
+
61
+ def original_url
62
+ PathHandler.new(options[:path], request_params).path
63
+ end
64
+
65
+ def url(params = {})
66
+ PathHandler.new(options[:path], request_params).with_params(params)
67
+ end
68
+
69
+ def get_page
70
+ request_params[:page]
71
+ end
72
+
73
+ def get_order
74
+ order = request_params[:order]
75
+
76
+ if order.blank?
77
+ nil
78
+ else
79
+ convert_mapping(options[:ordering][order.to_sym])
80
+ end
81
+ end
82
+
83
+ def get_direction
84
+ request_params[:asc] != "true" ? "DESC" : "ASC"
85
+ end
86
+
87
+ def convert_mapping(mapping)
88
+ # Check if mapping was provided
89
+ raise "No order mapping specified for '#{order}'" if mapping.blank?
90
+ # Convert to array in order to simplify processing
91
+ mapping = [mapping] if mapping.is_a?(String)
92
+ # Convert to SQL form
93
+ mapping.map {|m| "#{m} #{get_direction}"}.join(", ")
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,14 @@
1
+ class AdminBits::AdminResource::Paginator
2
+ attr_reader :resource, :page, :options
3
+
4
+ def initialize(resource, page, options)
5
+ @resource = resource
6
+ @page = page || 1
7
+ @options = options
8
+ end
9
+
10
+ def paginate
11
+ per_page = 30
12
+ resource.slice((page - 1) * per_page, page * per_page)
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ class AdminBits::AdminResource::PathHandler
2
+ attr_reader :request_params, :unprocessed_path
3
+
4
+ def initialize(path, request_params)
5
+ @request_params = request_params
6
+ @unprocessed_path = path
7
+ end
8
+
9
+ def path
10
+ @path ||= begin
11
+ if unprocessed_path.is_a?(Proc)
12
+ @path = routes.instance_exec(request_params, &unprocessed_path)
13
+ elsif unprocessed_path.is_a?(String)
14
+ @path = unprocessed_path
15
+ else
16
+ unknown_argument_type
17
+ end
18
+ end
19
+ end
20
+
21
+ def with_params(params = {})
22
+ path + "?" + request_params.merge(params).to_param
23
+ end
24
+
25
+ private
26
+
27
+ def routes
28
+ Rails.application.routes.url_helpers
29
+ end
30
+
31
+ def unknown_argument_type
32
+ raise "Wrong type of argument"
33
+ end
34
+ end