adminka 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +150 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/config/adminka_manifest.js +2 -0
  6. data/app/assets/images/adminka/logo.png +0 -0
  7. data/app/assets/images/adminka/no-pic.jpg +0 -0
  8. data/app/assets/javascripts/adminka/application.js +28 -0
  9. data/app/assets/stylesheets/adminka/application.scss +21 -0
  10. data/app/controllers/adminka/admin_controller.rb +32 -0
  11. data/app/controllers/adminka/admins/registrations_controller.rb +61 -0
  12. data/app/controllers/adminka/admins/sessions_controller.rb +26 -0
  13. data/app/controllers/adminka/crud_controller.rb +60 -0
  14. data/app/helpers/adminka/application_helper.rb +10 -0
  15. data/app/jobs/adminka/application_job.rb +4 -0
  16. data/app/mailers/adminka/application_mailer.rb +6 -0
  17. data/app/models/adminka/admin.rb +14 -0
  18. data/app/models/adminka/application_record.rb +5 -0
  19. data/app/views/adminka/admin/index.html.haml +1 -0
  20. data/app/views/adminka/admins/confirmations/new.html.haml +10 -0
  21. data/app/views/adminka/admins/mailer/confirmation_instructions.html.haml +4 -0
  22. data/app/views/adminka/admins/mailer/password_change.html.haml +3 -0
  23. data/app/views/adminka/admins/mailer/reset_password_instructions.html.haml +6 -0
  24. data/app/views/adminka/admins/mailer/unlock_instructions.html.haml +5 -0
  25. data/app/views/adminka/admins/passwords/edit.html.haml +19 -0
  26. data/app/views/adminka/admins/passwords/new.html.haml +10 -0
  27. data/app/views/adminka/admins/registrations/edit.html.haml +44 -0
  28. data/app/views/adminka/admins/registrations/new.html.haml +29 -0
  29. data/app/views/adminka/admins/sessions/new.html.haml +17 -0
  30. data/app/views/adminka/admins/shared/_links.html.haml +19 -0
  31. data/app/views/adminka/admins/unlocks/new.html.haml +10 -0
  32. data/app/views/adminka/crud/edit.html.haml +17 -0
  33. data/app/views/adminka/crud/index.html.haml +36 -0
  34. data/app/views/adminka/crud/new.html.haml +17 -0
  35. data/app/views/adminka/crud/show.html.haml +18 -0
  36. data/app/views/adminka/form_fields/_check_box.html.haml +4 -0
  37. data/app/views/adminka/form_fields/_image.html.haml +6 -0
  38. data/app/views/adminka/form_fields/_radio.html.haml +11 -0
  39. data/app/views/adminka/form_fields/_select.html.haml +9 -0
  40. data/app/views/adminka/form_fields/_text.html.haml +4 -0
  41. data/app/views/adminka/form_fields/_textarea.html.haml +4 -0
  42. data/app/views/kaminari/bootstrap/_first_page.html.haml +2 -0
  43. data/app/views/kaminari/bootstrap/_gap.html.haml +2 -0
  44. data/app/views/kaminari/bootstrap/_last_page.html.haml +2 -0
  45. data/app/views/kaminari/bootstrap/_next_page.html.haml +2 -0
  46. data/app/views/kaminari/bootstrap/_page.html.haml +6 -0
  47. data/app/views/kaminari/bootstrap/_paginator.html.haml +11 -0
  48. data/app/views/kaminari/bootstrap/_prev_page.html.haml +2 -0
  49. data/app/views/layouts/adminka/admin.html.haml +57 -0
  50. data/app/views/layouts/adminka/auth.html.haml +28 -0
  51. data/config/initializers/devise.rb +276 -0
  52. data/config/locales/adminka.en.yml +14 -0
  53. data/config/locales/devise.en.yml +62 -0
  54. data/config/routes.rb +8 -0
  55. data/db/migrate/20161225073001_devise_create_adminka_admins.rb +45 -0
  56. data/lib/adminka.rb +11 -0
  57. data/lib/adminka/engine.rb +9 -0
  58. data/lib/adminka/staff.rb +11 -0
  59. data/lib/adminka/version.rb +3 -0
  60. data/lib/generators/adminka/controllers_generator.rb +12 -0
  61. data/lib/generators/adminka/locale_generator.rb +12 -0
  62. data/lib/generators/adminka/views_generator.rb +17 -0
  63. data/lib/tasks/adminka_tasks.rake +4 -0
  64. metadata +250 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a34b2dd7f9d23fe3f3a59cfa2f3947c3966fcf2a
4
+ data.tar.gz: 28ab198bbe91d2439e667374da7bbeb0d1ea5071
5
+ SHA512:
6
+ metadata.gz: 614ef4ca93cf29c526af9dd20fb5e91b574ae59c04da4228dcd69ab44adcf90dea6ed642ba064ee083cfabca060e7af9adaba0d08943aea047a29bf4bfd1e38b
7
+ data.tar.gz: 2be02187948eb60c4121764d647d1f0aabf7c3193ce3a83a265b5d958a16fc9cac4d519b16337a99a60d8399d29953922fd450b971290edd67467ea9946689b2
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Igor
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,150 @@
1
+ # Adminka
2
+ Main goal of that plugin isn't to provide an admin dashboard with huge scope of functionality and
3
+ tasty features but to provided simple GUI for CRUD and ability to scale it with own features.
4
+ Plugin uses awesome [gem Devise](https://github.com/plataformatec/devise) for users' authentication,
5
+ [gem Kaminari](https://github.com/kaminari/kaminari) for pagination and [gem Carrierwave](https://github.com/carrierwaveuploader/carrierwave)
6
+ for file uploads.
7
+
8
+ ## Usage
9
+
10
+ After plugin migrations are done the next step will be mounting plugin to app routes. Insert in your routes.rb
11
+ ```ruby
12
+ mount Adminka::Engine => 'admin_panel', as: "adminka"
13
+ ```
14
+ Mounting path title is up to you(e.g. admin_panel, my_adminka etc.), the only thing should be
15
+ permanent is alias 'adminka'.
16
+
17
+ Congratulations! Now you can move to yourappdomain/admin_panel and you'll see login form. Of course at first you need to register
18
+ system admin. Fill in all standard fields and press Sign up. You will be redirected to root_path because newly created
19
+ admin should be activated to proceed. For activation, in your database you need to change in table adminka_admins field is_active to TRUE.
20
+ Only now your admin will be allowed to access main menu.
21
+
22
+ The next step will be a creation of proper resource to manage. Let it be user resource.
23
+ After resource creation you should pass routes to plugin by doing like this
24
+ ```ruby
25
+ Rails.application.routes.draw do
26
+
27
+ resources :cities
28
+
29
+ mount Adminka::Engine => 'admin_panel', as: "adminka"
30
+
31
+ Adminka::Engine.routes.draw do
32
+ resources :users
33
+ end
34
+ end
35
+ ```
36
+ and now you have restful routes to manage user resource. Then move to your controllers folder, create folder adminka and
37
+ and proper controller for you resource(e.g. users_controller).Don't forget that adminka controllers should be namespaced and inside
38
+ each resource controller you must initialize constant MODEL which holds name of proper model. See example below
39
+
40
+ ```ruby
41
+ class Adminka::UsersController < Adminka::CrudController
42
+ MODEL = 'User'
43
+ end
44
+ ```
45
+
46
+ Now let's provide an ability to upload avatar for users. Just install Carrierwave and follow installation instructions. As a result, for example,
47
+ you will have attribute avatar for User model and UserUploader. Well done! You can upload image for user.
48
+ The another point is pagination, it will help to manage huge amount of entities. So, we need to install Kaminari. Just add gem to your gemfile and
49
+ run bundle.That's all. The complete model should look like this:
50
+
51
+ ```ruby
52
+ class User < ApplicationRecord
53
+ paginates_per 5
54
+
55
+ belongs_to :city
56
+ mount_uploader :avatar, AvatarUploader
57
+
58
+ validates :first_name, :last_name, presence: true
59
+ end
60
+
61
+ ```
62
+
63
+
64
+ And the last step is creation model for resource. Adminka plugin uses .yml files to store models
65
+ data. Thus you need to move to your models folder, create folder adminka and to create user.yml file inside it.
66
+ You can see example in test/dummy/app/models/adminka directory. Below I'll provide short description of file structure:
67
+
68
+ 1. Parent field *user* - it should be equal to model name.
69
+ 2. *title* - left sidebar menu item title. It can be arbitrary(e.g. user, client etc).
70
+ 3. *attributes* - contains info about model attributes, their types and separate them for each resource actions(C,R,U,D).
71
+ 4. *list* - attributes, which will be shown by index action where resources list is present.
72
+ 5. *show* - attributes, which will be shown by show action.
73
+ 6. *edit* - attributes, which can be updated. Need to pay more attention to that feature. Each attribute should contain
74
+ it's title and type. Type title corresponds to html field types(e.g. radio, select, text etc). For some types you need to
75
+ provide more details:
76
+ * Type *radio* should contain field *value* and nested fields in format key: value
77
+ * Type *select* should have a foreign key name. For example, User belongs_to City, so the field name is city_id, then
78
+ *value* attribute contains proper parent model name(e.g. City), *attributes* field contains two subfields: *id*(mandatory field)
79
+ and field which contains parent model field value(e.g. *title* City title). So you have id and you want to get title of City by
80
+ using relation.
81
+ 7. If your field contains image url, just assign it to type: *image*.
82
+ 8. *search* - list of fields you want to provide search for(e.g. first_name, last_name)
83
+
84
+ **You can find model structure example in engine's root directory /test/dummy/app/models/adminka**
85
+
86
+ ## Views
87
+ Plugin uses some defaults views and layouts and of course you can change them according to your needs. For that purpose plugin provides
88
+ views generator, just run
89
+
90
+ ```bash
91
+ $ rails g adminka:views
92
+ ```
93
+ and you'll get all needed views in views/adminka directory and views/layouts/adminka.
94
+
95
+ ## Controllers
96
+ If you need custom behavior you can generate plugin controllers and override proper actions by running:
97
+
98
+ ```bash
99
+ $ rails g adminka:controllers
100
+ ```
101
+
102
+ ## I18n
103
+
104
+ To generate translation files run:
105
+
106
+ ```bash
107
+ $ rails g adminka:locale
108
+ ```
109
+
110
+ ## Installation
111
+
112
+ ```ruby
113
+ gem 'adminka'
114
+ ```
115
+ or if you prefer latest updates
116
+
117
+ ```ruby
118
+ gem 'adminka', github: 'rockandruby/adminka'
119
+ ```
120
+
121
+ And then execute:
122
+ ```bash
123
+ $ bundle
124
+ ```
125
+
126
+ Or install it yourself as:
127
+ ```bash
128
+ $ gem install adminka
129
+ ```
130
+
131
+ First of all after gem installation you need to copy migrations to your app by executing
132
+ ```bash
133
+ $ rails adminka:install:migrations
134
+ ```
135
+ and then migrate.
136
+
137
+ ## Demo
138
+
139
+ [Demo app](https://adminka.herokuapp.com)
140
+ ##### Test credentials:
141
+
142
+ > Login: bullet6666@mail.ru
143
+
144
+ > Password: 123456
145
+
146
+ ## Contributing
147
+ Contribution directions go here.
148
+
149
+ ## License
150
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Adminka'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/adminka .js
2
+ //= link_directory ../stylesheets/adminka .css
@@ -0,0 +1,28 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require bootstrap-sprockets
16
+ //= require_tree .
17
+
18
+ function readURL(input) {
19
+ if (input.files && input.files[0]) {
20
+ var reader = new FileReader();
21
+
22
+ reader.onload = function (e) {
23
+ $('#image_prev').attr('src', e.target.result)
24
+ };
25
+
26
+ reader.readAsDataURL(input.files[0]);
27
+ }
28
+ }
@@ -0,0 +1,21 @@
1
+ @import "bootstrap-sprockets";
2
+ @import "bootstrap";
3
+
4
+ .notice{
5
+ color: green;
6
+ }
7
+
8
+ #image_prev{
9
+ padding-top: 20px;
10
+ width: 20%;
11
+ }
12
+
13
+ .navbar{
14
+ background-color: #337ab7 !important;
15
+ }
16
+ h3, thead{
17
+ color: #337ab7 !important;
18
+ }
19
+ .dropdown-toggle{
20
+ color: white !important;
21
+ }
@@ -0,0 +1,32 @@
1
+ module Adminka
2
+ class AdminController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ before_action :check_auth, except: [:new, :create]
5
+
6
+ layout proc { admin_signed_in? ? 'adminka/admin' : 'adminka/auth' }
7
+
8
+ def initialize
9
+ super
10
+ @menu = {}
11
+ Dir.glob(Rails.root + 'app/models/adminka/*.yml') do |f|
12
+ @menu.merge! YAML.load(File.read(f))
13
+ end
14
+ end
15
+
16
+ protected
17
+
18
+ def check_auth
19
+ if !admin_signed_in?
20
+ redirect_to new_admin_session_path
21
+ elsif admin_signed_in? && !current_admin.is_active
22
+ flash[:alert] = t('adminka.activate_acc')
23
+ redirect_to '/'
24
+ end
25
+ end
26
+
27
+ def after_sign_out_path_for(resource_or_scope)
28
+ new_admin_session_path
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,61 @@
1
+ class Adminka::Admins::RegistrationsController < Devise::RegistrationsController
2
+ before_action :configure_sign_up_params, only: [:create]
3
+ before_action :configure_account_update_params, only: [:update]
4
+
5
+ # layout 'adminka/admin', only: :edit
6
+ # GET /resource/sign_up
7
+ # def new
8
+ # super
9
+ # end
10
+
11
+ # POST /resource
12
+ # def create
13
+ # super
14
+ # end
15
+
16
+ # GET /resource/edit
17
+ # def edit
18
+ # super
19
+ # end
20
+
21
+ # PUT /resource
22
+ # def update
23
+ # super
24
+ # end
25
+
26
+ # DELETE /resource
27
+ # def destroy
28
+ # super
29
+ # end
30
+
31
+ # GET /resource/cancel
32
+ # Forces the session data which is usually expired after sign
33
+ # in to be expired now. This is useful if the user wants to
34
+ # cancel oauth signing in/up in the middle of the process,
35
+ # removing all OAuth session data.
36
+ # def cancel
37
+ # super
38
+ # end
39
+
40
+ protected
41
+
42
+ # If you have extra params to permit, append them to the sanitizer.
43
+ def configure_sign_up_params
44
+ devise_parameter_sanitizer.permit(:sign_up, keys: %i(first_name last_name))
45
+ end
46
+
47
+ # If you have extra params to permit, append them to the sanitizer.
48
+ def configure_account_update_params
49
+ devise_parameter_sanitizer.permit(:account_update, keys: %i(first_name last_name))
50
+ end
51
+
52
+ # The path used after sign up.
53
+ # def after_sign_up_path_for(resource)
54
+ # super(resource)
55
+ # end
56
+
57
+ # The path used after sign up for inactive accounts.
58
+ # def after_inactive_sign_up_path_for(resource)
59
+ # super(resource)
60
+ # end
61
+ end
@@ -0,0 +1,26 @@
1
+ class Adminka::Admins::SessionsController < Devise::SessionsController
2
+ # before_action :configure_sign_in_params, only: [:create]
3
+ layout 'adminka/auth'
4
+
5
+ # GET /resource/sign_in
6
+ # def new
7
+ # super
8
+ # end
9
+
10
+ # POST /resource/sign_in
11
+ # def create
12
+ # super
13
+ # end
14
+
15
+ # DELETE /resource/sign_out
16
+ # def destroy
17
+ # super
18
+ # end
19
+
20
+ # protected
21
+
22
+ # If you have extra params to permit, append them to the sanitizer.
23
+ # def configure_sign_in_params
24
+ # devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
25
+ # end
26
+ end
@@ -0,0 +1,60 @@
1
+ module Adminka
2
+ class CrudController < AdminController
3
+ include Staff
4
+
5
+ before_action :check_auth
6
+
7
+ def initialize
8
+ super
9
+ @config = @menu[self.class::MODEL.downcase]
10
+ @model = Object.const_get(self.class::MODEL)
11
+ end
12
+
13
+ def index
14
+ @q = @model.ransack(params[:q])
15
+ @entities = @q.result.select(select_fields('list')).page(params[:page])
16
+ end
17
+
18
+ def create
19
+ @entity = @model.new(entity_params)
20
+ return render :new unless @entity.save
21
+ flash[:notice] = t('adminka.create')
22
+ redirect_to @entity
23
+ end
24
+
25
+ def new
26
+ @entity = @model.new
27
+ end
28
+
29
+ def show
30
+ @entity = @model.select(select_fields('show')).find(params[:id])
31
+ end
32
+
33
+ def edit
34
+ @entity = @model.select(select_fields('edit')).find(params[:id])
35
+ end
36
+
37
+ def update
38
+ @entity = @model.find(params[:id])
39
+ if @entity.update(entity_params)
40
+ flash[:notice] = t('adminka.update')
41
+ redirect_to @entity
42
+ else
43
+ render :edit
44
+ end
45
+ end
46
+
47
+ def destroy
48
+ @model.find(params[:id]).destroy
49
+ flash[:notice] = t('adminka.delete')
50
+ redirect_to adminka.send("#{@model.to_s.downcase.pluralize}_path")
51
+ end
52
+
53
+ private
54
+
55
+ def entity_params
56
+ params.require(@model.to_s.downcase.to_sym).permit(select_fields('edit'))
57
+ end
58
+
59
+ end
60
+ end