unknown_admin 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +57 -0
  3. data/Rakefile +38 -0
  4. data/app/controllers/admin/admin_controller.rb +60 -0
  5. data/app/controllers/admin/resources_controller.rb +44 -0
  6. data/app/controllers/admin/users_controller.rb +52 -0
  7. data/app/helpers/admin/resources_helper.rb +41 -0
  8. data/app/models/user.rb +4 -0
  9. data/app/views/admin/admin/index.html.erb +36 -0
  10. data/app/views/admin/resources/_breadcrumb.html.erb +6 -0
  11. data/app/views/admin/resources/_condition_fields.html.erb +10 -0
  12. data/app/views/admin/resources/_form.html.erb +4 -0
  13. data/app/views/admin/resources/_form_actions.html.erb +5 -0
  14. data/app/views/admin/resources/_form_fields.html.erb +4 -0
  15. data/app/views/admin/resources/_list.html.erb +13 -0
  16. data/app/views/admin/resources/_list_actions.html.erb +5 -0
  17. data/app/views/admin/resources/_list_header.html.erb +6 -0
  18. data/app/views/admin/resources/_list_item.html.erb +12 -0
  19. data/app/views/admin/resources/_list_item_actions.html.erb +11 -0
  20. data/app/views/admin/resources/_search_field.html.erb +31 -0
  21. data/app/views/admin/resources/edit.html.erb +19 -0
  22. data/app/views/admin/resources/index.html.erb +11 -0
  23. data/app/views/admin/resources/new.html.erb +19 -0
  24. data/app/views/admin/resources/show.html.erb +23 -0
  25. data/app/views/admin/users/_form.html.erb +102 -0
  26. data/app/views/admin/users/_form_new.html.erb +66 -0
  27. data/app/views/admin/users/token_fields.json.rabl +2 -0
  28. data/app/views/devise/confirmations/new.html.erb +12 -0
  29. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  30. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  31. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  32. data/app/views/devise/passwords/edit.html.erb +16 -0
  33. data/app/views/devise/passwords/new.html.erb +12 -0
  34. data/app/views/devise/registrations/edit.html.erb +25 -0
  35. data/app/views/devise/registrations/new.html.erb +18 -0
  36. data/app/views/devise/sessions/new.html.erb +17 -0
  37. data/app/views/devise/shared/_links.erb +25 -0
  38. data/app/views/devise/unlocks/new.html.erb +12 -0
  39. data/app/views/layouts/admin/application.html.erb +31 -0
  40. data/config/routes.rb +8 -0
  41. data/lib/generators/unknown_admin/controller/controller_generator.rb +68 -0
  42. data/lib/generators/unknown_admin/controller/templates/standard_controller.rb +12 -0
  43. data/lib/generators/unknown_admin/install/install_generator.rb +36 -0
  44. data/lib/generators/unknown_admin/install/templates/admin.css +17 -0
  45. data/lib/generators/unknown_admin/install/templates/admin.js +20 -0
  46. data/lib/generators/unknown_admin/install/templates/admin_controller.rb +63 -0
  47. data/lib/generators/unknown_admin/install/templates/application.html.erb +39 -0
  48. data/lib/generators/unknown_admin/install/templates/resources_controller.rb +44 -0
  49. data/lib/tasks/unknown_admin_tasks.rake +4 -0
  50. data/lib/unknown_admin.rb +17 -0
  51. data/lib/unknown_admin/version.rb +3 -0
  52. data/test/dummy/README.rdoc +261 -0
  53. data/test/dummy/Rakefile +7 -0
  54. data/test/dummy/app/assets/javascripts/application.js +15 -0
  55. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  56. data/test/dummy/app/controllers/application_controller.rb +3 -0
  57. data/test/dummy/app/helpers/application_helper.rb +2 -0
  58. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  59. data/test/dummy/config.ru +4 -0
  60. data/test/dummy/config/application.rb +59 -0
  61. data/test/dummy/config/boot.rb +10 -0
  62. data/test/dummy/config/database.yml +25 -0
  63. data/test/dummy/config/environment.rb +5 -0
  64. data/test/dummy/config/environments/development.rb +37 -0
  65. data/test/dummy/config/environments/production.rb +67 -0
  66. data/test/dummy/config/environments/test.rb +37 -0
  67. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  68. data/test/dummy/config/initializers/inflections.rb +15 -0
  69. data/test/dummy/config/initializers/mime_types.rb +5 -0
  70. data/test/dummy/config/initializers/secret_token.rb +7 -0
  71. data/test/dummy/config/initializers/session_store.rb +8 -0
  72. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  73. data/test/dummy/config/locales/en.yml +5 -0
  74. data/test/dummy/config/routes.rb +58 -0
  75. data/test/dummy/public/404.html +26 -0
  76. data/test/dummy/public/422.html +26 -0
  77. data/test/dummy/public/500.html +25 -0
  78. data/test/dummy/public/favicon.ico +0 -0
  79. data/test/dummy/script/rails +6 -0
  80. data/test/test_helper.rb +15 -0
  81. data/test/unknown_admin_test.rb +7 -0
  82. metadata +290 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
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,57 @@
1
+ # unknown? Admin Backend
2
+ `unknown-admin` is a basic framework for administration backend purposes. It is heavily based on Bootstrap and inherited_resources.
3
+ It is focused on the DRY principle throughout.
4
+
5
+ Through template inheritance there is hardly more to do, than creating an empty controller for your model.
6
+ It completely implements all CRUD methods and provides default views for them.
7
+ If you want, you can override a lot, of course.
8
+
9
+
10
+ Enjoy.
11
+
12
+ ## Usage
13
+
14
+ ### Rails
15
+
16
+ In your Gemfile:
17
+
18
+ ### Required for bootstrap (starting Rails 3.2.9)
19
+
20
+ gem "therubyracer"
21
+ gem "less-rails"
22
+ gem "twitter-bootstrap-rails"
23
+
24
+ ### Add the `unknown-admin` gem
25
+
26
+ gem 'unknown_admin',:git=>"http://gitlab.unknown.de/unknown_admin.git"
27
+
28
+
29
+ ## Installation
30
+
31
+ Run `bundle install` from the command line:
32
+
33
+ bundle install
34
+
35
+ It is possible that v8 engine libraries need to be installed
36
+
37
+ brew install v8
38
+
39
+ Then run the boostrap generator to add Bootstrap includes into your assets:
40
+
41
+ rails generate bootstrap:install less
42
+
43
+ Also run the unknown_admin generator to install the asset files for the admin-backend
44
+
45
+ rails generate unknown_admin:install
46
+
47
+ Add following line to one of your initializers, if you want to use strong parameters
48
+
49
+ ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
50
+
51
+ You can generate an admin controller by using... model must exist for this to work!
52
+
53
+ rails g unknown_admin:controller ModelName
54
+
55
+ If you do want to add your routes manually, use this:
56
+
57
+ rails g unknown_admin:controller ModelName --skip-routes
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
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
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'UnknownAdmin'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,60 @@
1
+ module Admin
2
+ class AdminController < ActionController::Base
3
+
4
+ #before_filter :authenticate_user!
5
+ protect_from_forgery
6
+
7
+ layout :ajax_request
8
+ #before_filter :authorize
9
+
10
+ #delegate :allow?, to: :current_permission
11
+ helper_method :allow?
12
+
13
+ #delegate :allow_param?, to: :current_permission
14
+ #helper_method :allow_param?
15
+
16
+
17
+ #helper_method :current_domain
18
+ helper_method :current_style
19
+
20
+
21
+
22
+ private
23
+ def ajax_request
24
+ request.xhr? ? "admin/ajax" : "admin/application"
25
+ end
26
+
27
+ ## TODO: Fix this
28
+ def allow?(*args)
29
+ true
30
+ end
31
+
32
+ def current_domain
33
+ @current_domain ||= Domain.find(session[:current_domain_id]) unless session[:current_domain_id].nil?
34
+ end
35
+ def current_style
36
+ color = "#fff"
37
+
38
+ templ=Tilt::ERBTemplate.new { "body { background-color:<%=color%>}" }
39
+ return templ.render(self,:color=>color)
40
+ end
41
+
42
+ def current_permission
43
+ @current_permission ||= Permissions.permission_for(current_user)
44
+ end
45
+
46
+ def current_resource
47
+ nil
48
+ end
49
+
50
+ def authorize
51
+ User.current=current_user
52
+ if current_permission.allow?(params[:controller], params[:action], current_resource)
53
+ current_permission.permit_params! params
54
+ else
55
+ render :text=> "Not authorized.",:status=>:unauthorized
56
+ end
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,44 @@
1
+ require 'inherited_resources'
2
+ module Admin
3
+ class ResourcesController < AdminController
4
+
5
+ inherit_resources
6
+
7
+ helper_method :list_fields
8
+ def list_fields
9
+ resource_class.column_names
10
+ end
11
+
12
+ helper_method :show_fields
13
+ def show_fields
14
+ (resource_class.column_names-["id","created_by","updated_by","created_at","updated_at"])
15
+ end
16
+
17
+ helper_method :search_fields
18
+ def search_fields
19
+ []
20
+ end
21
+
22
+
23
+ def create
24
+ create! { collection_path }
25
+ end
26
+ def update
27
+ update! { collection_path }
28
+ end
29
+
30
+
31
+
32
+ protected
33
+
34
+ def collection
35
+ @search = end_of_association_chain.search(params[:q])
36
+ get_collection_ivar || set_collection_ivar(@search.result.paginate(:page => params[:page], :per_page => per_page))
37
+ end
38
+
39
+ def per_page
40
+ 10
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,52 @@
1
+ module Admin
2
+ class UsersController < ResourcesController
3
+ helper_method :default_fields
4
+ #crudify :user,:paginate => true
5
+
6
+ respond_to :html
7
+
8
+ def list_fields
9
+ [:id, :email, :first_name, :last_name]
10
+ end
11
+
12
+ def search_fields
13
+ [:email_cont]
14
+ end
15
+
16
+ def default_fields(model)
17
+ fields= []
18
+ fields[:user]=[:user_id, :login_id]
19
+ end
20
+
21
+
22
+
23
+
24
+ # GET /users/1/render/:partial
25
+ def partial
26
+ @user = User.find(params[:id])
27
+ render :partial=>params[:partial]
28
+ end
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+ private
37
+
38
+ def pre_select_users
39
+ if current_domain.nil?
40
+ @users = User
41
+ else
42
+ @users = current_domain.users.master
43
+ end
44
+ end
45
+
46
+
47
+
48
+
49
+
50
+
51
+ end
52
+ end
@@ -0,0 +1,41 @@
1
+ module Admin
2
+ module ResourcesHelper
3
+ def resolve_field(resource,list_field_item)
4
+ resolved = list_field_item.class.to_s
5
+ case list_field_item
6
+ when Symbol,String
7
+ val = resource.send(list_field_item)
8
+ case val
9
+ when ActiveSupport::TimeWithZone
10
+ resolved =l(val,format: :long)
11
+ else
12
+ resolved = val
13
+ end
14
+ when Hash
15
+ rel = resource.send(list_field_item.first.first)
16
+ resolved = rel.send(list_field_item.first.last)
17
+ end
18
+ resolved
19
+ end
20
+ def resolve_field_name(list_field_item)
21
+ case list_field_item
22
+ when Symbol,String
23
+ resource_class.human_attribute_name(list_field_item)
24
+ when Hash
25
+ resource_class.human_attribute_name(list_field_item.first.first)
26
+ end
27
+ end
28
+
29
+ def link_to_add_fields(name, f, type)
30
+ new_object = f.object.send "build_#{type}"
31
+ id = "new_#{type}"
32
+ fields = f.send("#{type}_fields", new_object, child_index: id) do |builder|
33
+ render(type.to_s + "_fields", f: builder)
34
+ end
35
+ link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
36
+ end
37
+
38
+
39
+
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+
3
+
4
+ end
@@ -0,0 +1,36 @@
1
+ <%- model_class = Role -%>
2
+ <div class="page-header">
3
+ <h1><%=t '.title', :default => model_class.model_name.human.pluralize %></h1>
4
+ </div>
5
+ <table class="table table-striped table-bordered table-condensed">
6
+ <thead>
7
+ <tr>
8
+ <% index_fields do |field| %>
9
+ <th><%= model_class.human_attribute_name(field) %></th>
10
+ <% end %>
11
+ <th><%=t '.actions', :default => t("helpers.actions") %></th>
12
+ </tr>
13
+ </thead>
14
+ <tbody>
15
+ <% @roles.each do |role| %>
16
+ <tr>
17
+ <td><%= link_to role.role_id, admin_role_path(role) %></td>
18
+ <td><%= role.role_name %></td>
19
+ <td><%= role.domain_role %></td>
20
+ <td>
21
+ <%= link_to t('.edit', :default => t("helpers.links.edit")),
22
+ edit_admin_role_path(role), :class => 'btn btn-mini' %>
23
+ <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
24
+ admin_role_path(role),
25
+ :method => :delete,
26
+ :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
27
+ :class => 'btn btn-mini btn-danger' %>
28
+ </td>
29
+ </tr>
30
+ <% end %>
31
+ </tbody>
32
+ </table>
33
+
34
+ <%= link_to t('.new', :default => t("helpers.links.new")),
35
+ new_admin_role_path,
36
+ :class => 'btn btn-primary' %>
@@ -0,0 +1,6 @@
1
+ <ul class="breadcrumb">
2
+ <li><%= link_to "Home",admin_root_url %><span class="divider">/</span></li>
3
+ <% association_chain[0..-1].each do |ancestor| %>
4
+ <li><%= link_to ancestor.class.model_name.human.pluralize,[:admin]+association_chain[0..(association_chain.find_index(ancestor)-(association_chain.count+1))]+[ancestor.class] %><span class="divider">/</span></li>
5
+ <% end %>
6
+ </ul>
@@ -0,0 +1,10 @@
1
+ <div class="field">
2
+ <%= f.attribute_fields do |a| %>
3
+ <%= a.attribute_select({associations: [:media_source]},{class:"chosen_field"}) %>
4
+ <% end %>
5
+ <%= f.predicate_select({},{class:"chosen_field"}) %>
6
+ <%= f.value_fields do |v| %>
7
+ <%= v.text_field :value %>
8
+ <% end %>
9
+ <%= link_to "remove", '#', class: "remove_fields" %>
10
+ </div>
@@ -0,0 +1,4 @@
1
+ <%= simple_form_for [:admin]+association_chain+[resource], :html => { :class => 'form-horizontal', :multipart => true } do |f| %>
2
+ <%= render :partial=>"form_fields",locals:{f:f} %>
3
+ <%= render :partial=>"form_actions",:locals=>{f: f, resource: resource} %>
4
+ <% end %>
@@ -0,0 +1,5 @@
1
+ <div class="form-actions">
2
+ <%= f.submit nil, :class => 'btn btn-primary' %>
3
+ <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
4
+ :back, :class => 'btn' %>
5
+ </div>
@@ -0,0 +1,4 @@
1
+ <%= f.input :id, disabled:true %>
2
+ <% show_fields.each do |field| %>
3
+ <%= f.input field %>
4
+ <% end %>
@@ -0,0 +1,13 @@
1
+ <p><span class="badge"><%= collection.total_entries %> Records found</span></p>
2
+ <% if resource_class.column_names.include?('position') %>
3
+ <table id="sortable_list" class="table table-striped table-bordered table-condensed" data-update-url="<%= list_sort_resources_path %>">
4
+ <% else %>
5
+ <table id="sortable_list" class="table table-striped table-bordered table-condensed">
6
+ <% end %>
7
+ <thead>
8
+ <%= render :partial=>"list_header" %>
9
+ </thead>
10
+ <tbody>
11
+ <%= render :partial=>"list_item",collection: collection %>
12
+ </tbody>
13
+ </table>
@@ -0,0 +1,5 @@
1
+ <% if allow? "admin/#{resource_class.model_name.pluralize.underscore}","new" %>
2
+ <%= link_to t('.new', :default => t("helpers.links.new")),
3
+ new_resource_path(:admin),
4
+ :class => 'btn btn-primary' %>
5
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <tr>
2
+ <% list_fields.each do |field| %>
3
+ <th><%= sort_link @search, field, resolve_field_name(field) %></th>
4
+ <% end %>
5
+ <th><%=t '.actions', :default => t("helpers.actions") %></th>
6
+ </tr>
@@ -0,0 +1,12 @@
1
+ <tr class="list-item" id="<%= dom_id(list_item) %>">
2
+ <% list_fields.each_with_index do |field,index| %>
3
+ <% if index == 0 %>
4
+ <td><%= link_to resolve_field(list_item,field), resource_path(list_item) %></td>
5
+ <% else %>
6
+ <td><%= resolve_field(list_item,field) %></td>
7
+ <% end %>
8
+ <% end %>
9
+ <td>
10
+ <%= render partial: "list_item_actions",locals:{resource:list_item} %>
11
+ </td>
12
+ </tr>