padrino-admin 0.6.3 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/README.rdoc +16 -0
  2. data/VERSION +1 -1
  3. data/lib/padrino-admin.rb +24 -5
  4. data/lib/padrino-admin/access_control.rb +75 -29
  5. data/lib/padrino-admin/{ext_js/column_store.rb → column_store.rb} +31 -15
  6. data/lib/padrino-admin/config.rb +36 -0
  7. data/lib/padrino-admin/generators/actions.rb +62 -7
  8. data/lib/padrino-admin/generators/admin_app.rb +27 -26
  9. data/lib/padrino-admin/generators/admin_page.rb +20 -18
  10. data/lib/padrino-admin/generators/admin_uploader.rb +83 -0
  11. data/lib/padrino-admin/generators/app/app.rb.tt +1 -3
  12. data/lib/padrino-admin/generators/app/controllers/accounts.rb +1 -1
  13. data/lib/padrino-admin/generators/app/public/flash/swfupload.swf +0 -0
  14. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/back.gif +0 -0
  15. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/background.png +0 -0
  16. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-content.png +0 -0
  17. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-hd-slate.png +0 -0
  18. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-hd.png +0 -0
  19. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-intro.png +0 -0
  20. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-login.png +0 -0
  21. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-menu-slate.png +0 -0
  22. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-menu.png +0 -0
  23. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg.png +0 -0
  24. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/btn-login.png +0 -0
  25. data/lib/padrino-admin/generators/app/public/images/admin/cancel.gif +0 -0
  26. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/categories.gif +0 -0
  27. data/lib/padrino-admin/generators/app/public/images/admin/close.gif +0 -0
  28. data/lib/padrino-admin/generators/app/public/images/admin/close.png +0 -0
  29. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/delete.gif +0 -0
  30. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/download.gif +0 -0
  31. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/duplicate.gif +0 -0
  32. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/edit.gif +0 -0
  33. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/export.gif +0 -0
  34. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/hd-bg.gif +0 -0
  35. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/image.gif +0 -0
  36. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/loader.gif +0 -0
  37. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/logo-loader.png +0 -0
  38. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/logo-small.png +0 -0
  39. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/new.gif +0 -0
  40. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/no-image.png +0 -0
  41. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/preview.gif +0 -0
  42. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/print.gif +0 -0
  43. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/save.gif +0 -0
  44. data/lib/padrino-admin/generators/app/public/images/{backend → admin}/support.gif +0 -0
  45. data/lib/padrino-admin/generators/app/public/images/admin/up.gif +0 -0
  46. data/lib/padrino-admin/generators/app/public/javascripts/ext.js +4 -1
  47. data/lib/padrino-admin/generators/app/public/javascripts/swfupload.js +4 -0
  48. data/lib/padrino-admin/generators/app/public/stylesheets/admin.css +25 -43
  49. data/lib/padrino-admin/generators/app/public/stylesheets/login.css +3 -3
  50. data/lib/padrino-admin/generators/app/public/stylesheets/standard.css +53 -52
  51. data/lib/padrino-admin/generators/app/views/accounts/_form.haml +1 -0
  52. data/lib/padrino-admin/generators/app/views/base/index.haml +5 -7
  53. data/lib/padrino-admin/generators/app/views/javascripts/admin.js.erb +217 -297
  54. data/lib/padrino-admin/generators/app/views/sessions/new.haml +1 -1
  55. data/lib/padrino-admin/generators/templates/{controller.rb.tt → page/controller.rb.tt} +1 -1
  56. data/lib/padrino-admin/generators/templates/{db → page/db}/seeds.rb.tt +0 -0
  57. data/lib/padrino-admin/generators/templates/{views → page/views}/_form.haml.tt +0 -0
  58. data/lib/padrino-admin/generators/templates/{views → page/views}/edit.haml.tt +0 -0
  59. data/lib/padrino-admin/generators/templates/{views → page/views}/grid.js.erb.tt +1 -1
  60. data/lib/padrino-admin/generators/templates/{views → page/views}/new.haml.tt +0 -0
  61. data/lib/padrino-admin/generators/templates/{views → page/views}/store.jml.tt +0 -0
  62. data/lib/padrino-admin/generators/templates/uploader/controller.rb +24 -0
  63. data/lib/padrino-admin/generators/templates/uploader/lib/uploader.rb +54 -0
  64. data/lib/padrino-admin/generators/templates/uploader/views/grid.js.erb +56 -0
  65. data/lib/padrino-admin/generators/templates/uploader/views/store.jml +10 -0
  66. data/lib/padrino-admin/helpers/authentication.rb +30 -19
  67. data/lib/padrino-admin/helpers/view.rb +172 -35
  68. data/lib/padrino-admin/locale/admin/en.yml +1 -0
  69. data/lib/padrino-admin/middleware/flash_middleware.rb +36 -0
  70. data/lib/padrino-admin/orm.rb +33 -0
  71. data/lib/padrino-admin/orm/abstract.rb +94 -0
  72. data/lib/padrino-admin/{adapters/ar.rb → orm/activerecord.rb} +69 -36
  73. data/lib/padrino-admin/orm/datamapper.rb +214 -0
  74. data/lib/padrino-admin/{adapters/mm.rb → orm/mongomapper.rb} +36 -20
  75. data/lib/padrino-admin/utils/literal.rb +1 -1
  76. data/padrino-admin.gemspec +62 -51
  77. data/test/fixtures/active_record.rb +14 -2
  78. data/test/fixtures/data_mapper.rb +2 -1
  79. data/test/fixtures/mongo_mapper.rb +1 -1
  80. data/test/fixtures/test_column_store.jml +1 -0
  81. data/test/helper.rb +3 -2
  82. data/test/test_access_control.rb +1 -1
  83. data/test/test_active_record.rb +56 -1
  84. data/test/test_column_store.rb +56 -10
  85. data/test/test_data_mapper.rb +67 -1
  86. metadata +58 -47
  87. data/lib/padrino-admin/adapters.rb +0 -108
  88. data/lib/padrino-admin/adapters/dm.rb +0 -147
  89. data/lib/padrino-admin/ext_js/config.rb +0 -186
  90. data/test/fixtures/test_generic.jml +0 -7
  91. data/test/fixtures/test_javascript.jml +0 -81
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'yaml'
3
+ require 'erb'
4
+ require 'json/pure' unless defined?(JSON) || defined?(JSON::Pure)
5
+
6
+ module Padrino
7
+ module Admin
8
+ module Config
9
+
10
+ ##
11
+ # This class it's used for JSON variables.
12
+ # Normally if we convert this { :function => "alert('Test')" } will be:
13
+ #
14
+ # { "function": "alert('Test')" }
15
+ #
16
+ # But if in our javascript need to "eval" this function is not possible because
17
+ # it's a string.
18
+ #
19
+ # Using Padrino::Config::Variable the result will be:
20
+ #
21
+ # { "function" : alert('Test') }
22
+ #
23
+ # Normally an ExtJs Variable can be handled with ExtJs Config like:
24
+ #
25
+ # function: !js alert('Test')
26
+ #
27
+ class Variable < String
28
+ yaml_as "tag:yaml.org,2002:js"
29
+
30
+ def to_json(*a) #:nodoc:
31
+ self
32
+ end
33
+ end # Variable
34
+ end # Config
35
+ end # Admin
36
+ end # Padrino
@@ -1,16 +1,71 @@
1
1
  module Padrino
2
2
  module Generators
3
3
  module Actions
4
- # For access control permissions
5
- def access_control(name)
6
- (<<-RUBY)
7
- role.project_module :#{name} do |project|
8
- project.menu :list, "/admin/#{name}.js"
9
- project.menu :new, "/admin/#{name}/new"
4
+ ##
5
+ # Tell us if for our orm we need migrations
6
+ #
7
+ def skip_migrations
8
+ skip_migration = case orm
9
+ when :activerecord then false
10
+ when :sequel then false
11
+ else true
12
+ end
10
13
  end
11
14
 
12
- RUBY
15
+ ##
16
+ # Tell us which orm we are using
17
+ #
18
+ def orm
19
+ fetch_component_choice(:orm).to_sym rescue :datamapper
13
20
  end
21
+
22
+ ##
23
+ # Tell us for now wich orm we support
24
+ #
25
+ def supported_orm
26
+ [:datamapper, :activerecord]
27
+ end
28
+
29
+ ##
30
+ # Add access_control permission in our app.rb
31
+ #
32
+ def add_access_control_permission(admin, controller)
33
+ permission = indent(6, access_control(controller))
34
+ if options[:destroy] || !File.read(destination_root("#{admin}/app.rb")).include?(permission)
35
+ inject_into_file destination_root("#{admin}/app.rb"), permission, :after => "access_control.roles_for :admin do |role, account|\n"
36
+ end
37
+ end
38
+
39
+ ##
40
+ # Add a simple permission (allow/deny) to our app.rb
41
+ #
42
+ def add_permission(admin, permission)
43
+ if options[:destroy] || !File.read(destination_root("#{admin}/app.rb")).include?(permission)
44
+ inject_into_file destination_root("#{admin}/app.rb"), indent(6, "\n#{permission}\n"), :after => "access_control.roles_for :admin do |role, account|\n"
45
+ end
46
+ end
47
+
48
+ ##
49
+ # Indent a content/string for the given spaces
50
+ #
51
+ def indent(count, content)
52
+ indent = ' ' * count
53
+ content.map { |line| line != "\n" ? indent+line : "\n" }.join
54
+ end
55
+
56
+ private
57
+ ##
58
+ # For access control permissions
59
+ #
60
+ def access_control(controller)
61
+ (<<-RUBY).gsub(/ {12}/,'')
62
+
63
+ role.project_module :#{controller} do |project|
64
+ project.menu :list, "/admin/#{controller}.js"
65
+ project.menu :new, "/admin/#{controller}/new"
66
+ end
67
+ RUBY
68
+ end
14
69
  end
15
70
  end
16
71
  end
@@ -8,58 +8,59 @@ module Padrino
8
8
 
9
9
  # Define the source template root
10
10
  def self.source_root; File.expand_path(File.dirname(__FILE__)); end
11
- def self.banner; "padrino-gen backend"; end
11
+ def self.banner; "padrino-gen admin"; end
12
12
 
13
13
  # Include related modules
14
14
  include Thor::Actions
15
15
  include Padrino::Generators::Actions
16
16
 
17
- desc "Description:\n\n\tpadrino-gen controller generates a new Padrino Admin"
17
+ desc "Description:\n\n\tpadrino-gen admin generates a new Padrino Admin"
18
18
 
19
- class_option :root, :aliases => '-r', :default => ".", :type => :string
19
+ class_option :root, :desc => "The root destination", :aliases => '-r', :default => ".", :type => :string
20
20
  class_option :path, :aliases => '-p', :type => :string, :default => "admin"
21
21
  class_option :destroy, :aliases => '-d', :default => false, :type => :boolean
22
22
 
23
23
  # Copies over the Padrino base admin application
24
24
  def create_admin
25
- if in_app_root?(options[:root])
25
+ self.destination_root = options[:root]
26
+ if in_app_root?
26
27
  @app_path = options[:path]
27
- @orm = fetch_component_choice(:orm, options[:root]).to_sym rescue :datamapper
28
- supported_orm = [:datamapper, :activerecord]
29
- skip_migration = case @orm
30
- when :activerecord then false
31
- when :sequel then false
32
- else true
33
- end
34
28
 
35
- unless supported_orm.include?(@orm)
29
+ unless supported_orm.include?(orm)
36
30
  say "<= A the moment we only support #{supported_orm.join(" or ")}. Sorry!"
37
31
  raise SystemExit
38
32
  end
39
33
 
40
34
  self.behavior = :revoke if options[:destroy]
41
- directory("app/", File.join(options[:path]))
35
+ directory("app/", destination_root(options[:path]))
42
36
 
43
37
  Padrino::Generators::Model.dup.start([
44
38
  "account", "name:string", "surname:string", "email:string", "crypted_password:string", "salt:string", "role:string",
45
- "-r=#{options[:root]}", "-s=#{skip_migration}", "-d=#{options[:destroy]}"
39
+ "-r=#{options[:root]}", "-s=#{skip_migrations}", "-d=#{options[:destroy]}"
46
40
  ])
47
41
 
48
- template "templates/db/seeds.rb.tt", app_root_path("/db/seeds.rb")
42
+ insert_into_gemfile("haml")
43
+
44
+ template "templates/page/db/seeds.rb.tt", destination_root("/db/seeds.rb")
49
45
 
50
- if options[:destroy] || !File.read(app_root_path("config/apps.rb")).include?("Padrino.mount(\"Admin\").to(\"/#{@app_path}\")")
51
- append_file app_root_path("config/apps.rb"), "\nPadrino.mount(\"Admin\").to(\"/#{@app_path}\")"
46
+ if options[:destroy] || !File.read(destination_root("config/apps.rb")).include?("Padrino.mount(\"Admin\").to(\"/#{@app_path}\")")
47
+ append_file destination_root("config/apps.rb"), "\nPadrino.mount(\"Admin\").to(\"/#{@app_path}\")"
52
48
  end
53
-
49
+
54
50
  unless options[:destroy]
55
- say ""
56
- say "Your admin now is installed, now follow this steps:"
57
- say ""
58
- say " - edit your config/database.rb"
59
- say " - run padrino rake -T and run db creation according to your orm"
60
- say " - run padrino rake seed"
61
- say ""
62
- say "That's all"
51
+ say (<<-TEXT).gsub(/ {12}/,'')
52
+
53
+ -----------------------------------------------------------------
54
+ Your admin now is installed, now follow this steps:
55
+
56
+ - edit your config/database.rb
57
+ - run padrino rake -T and run db creation according to your orm
58
+ - run padrino rake seed
59
+
60
+ That's all
61
+ -----------------------------------------------------------------
62
+
63
+ TEXT
63
64
  end
64
65
  else
65
66
  say "You are not at the root of a Padrino application! (config/boot.rb not found)" and exit unless in_app_root?
@@ -8,16 +8,16 @@ module Padrino
8
8
 
9
9
  # Define the source template root
10
10
  def self.source_root; File.expand_path(File.dirname(__FILE__)); end
11
- def self.banner; "padrino-gen backend_page [Model]"; end
11
+ def self.banner; "padrino-gen admin_page [Model]"; end
12
12
 
13
13
  # Include related modules
14
14
  include Thor::Actions
15
15
  include Padrino::Generators::Actions
16
16
 
17
- desc "Description:\n\n\tpadrino-gen backend_page YourModel"
17
+ desc "Description:\n\n\tpadrino-gen admin_page YourModel"
18
18
  argument :model, :desc => "The name of your model"
19
19
  class_option :admin_path, :aliases => '-p', :type => :string, :default => "admin"
20
- class_option :root, :aliases => '-r', :type => :string
20
+ class_option :root, :desc => "The root destination", :aliases => '-r', :type => :string
21
21
  class_option :destroy, :aliases => '-d', :default => false, :type => :boolean
22
22
 
23
23
  # Show help if no argv given
@@ -28,23 +28,25 @@ module Padrino
28
28
 
29
29
  # Create controller for admin
30
30
  def create_controller
31
- if in_app_root?(options[:root])
32
- @model_name = model
33
- @model_klass = model.constantize
34
- @model_plural = model.to_s.downcase.pluralize
35
- @model_singular = model.to_s.downcase
31
+ self.destination_root = options[:root]
32
+ if in_app_root?
33
+ @model_name = model.classify
34
+ @model_klass = model.classify.constantize
35
+ @model_plural = model.underscore.pluralize
36
+ @model_singular = model.underscore
36
37
  @app_root = File.join(options[:root] || '.', options[:admin_path])
37
38
  self.behavior = :revoke if options[:destroy]
38
- template "templates/controller.rb.tt", app_root_path(options[:admin_path], "/controllers/#{@model_plural}.rb")
39
- template "templates/views/_form.haml.tt", app_root_path(options[:admin_path], "/views/#{@model_plural}/_form.haml")
40
- template "templates/views/edit.haml.tt", app_root_path(options[:admin_path], "/views/#{@model_plural}/edit.haml")
41
- template "templates/views/grid.js.erb.tt", app_root_path(options[:admin_path], "/views/#{@model_plural}/grid.js.erb")
42
- template "templates/views/new.haml.tt", app_root_path(options[:admin_path], "/views/#{@model_plural}/new.haml")
43
- template "templates/views/store.jml.tt", app_root_path(options[:admin_path], "/views/#{@model_plural}/store.jml")
44
- inject_into_file app_root_path("#{options[:admin_path]}/app.rb"), access_control(@model_plural), :before => " # Put before other permissions [don't delete this line!!!]"
45
- empty_directory app_root_path(options[:admin_path], "/views/#{@model_plural}")
46
- include_component_module_for(:test, options[:root])
47
- generate_controller_test(model.downcase.pluralize, options[:root])
39
+
40
+ template "templates/page/controller.rb.tt", destination_root(options[:admin_path], "/controllers/#{@model_plural}.rb")
41
+ template "templates/page/views/_form.haml.tt", destination_root(options[:admin_path], "/views/#{@model_plural}/_form.haml")
42
+ template "templates/page/views/edit.haml.tt", destination_root(options[:admin_path], "/views/#{@model_plural}/edit.haml")
43
+ template "templates/page/views/grid.js.erb.tt", destination_root(options[:admin_path], "/views/#{@model_plural}/grid.js.erb")
44
+ template "templates/page/views/new.haml.tt", destination_root(options[:admin_path], "/views/#{@model_plural}/new.haml")
45
+ template "templates/page/views/store.jml.tt", destination_root(options[:admin_path], "/views/#{@model_plural}/store.jml")
46
+
47
+ add_access_control_permission(options[:admin_path], @model_plural)
48
+ include_component_module_for(:test)
49
+ generate_controller_test(model.downcase.pluralize)
48
50
  else
49
51
  say "You are not at the root of a Padrino application! (config/boot.rb not found)" and return unless in_app_root?
50
52
  end
@@ -0,0 +1,83 @@
1
+ module Padrino
2
+ module Generators
3
+
4
+ class AdminUploader < Thor::Group
5
+
6
+ # Add this generator to our padrino-gen
7
+ Padrino::Generators.add_generator(:admin_uploader, self)
8
+
9
+ # Define the source template root
10
+ def self.source_root; File.expand_path(File.dirname(__FILE__)); end
11
+ def self.banner; "padrino-gen admin_uploader"; end
12
+
13
+ # Include related modules
14
+ include Thor::Actions
15
+ include Padrino::Generators::Actions
16
+
17
+ desc "Description:\n\n\tpadrino-gen admin_uploader Name"
18
+ class_option :admin_path, :desc => "Path where is stored your admin app", :aliases => '-p', :type => :string, :default => "admin"
19
+ class_option :root, :desc => "The root destination", :aliases => '-r', :type => :string, :default => "."
20
+ class_option :destroy, :desc => "Destroy the uploader", :aliases => '-d', :default => false, :type => :boolean
21
+
22
+ # Create controller for admin
23
+ def create_controller
24
+ self.destination_root = options[:root]
25
+ if in_app_root?
26
+ @app_root = File.join(options[:root], options[:admin_path])
27
+ self.behavior = :revoke if options[:destroy]
28
+
29
+ if options[:destroy] || !File.read(destination_root("GemFile")).include?("carrierwave")
30
+ append_file destination_root("Gemfile"), "\n\n# Uploader requirements\ngem 'carrierwave'"
31
+ end
32
+
33
+ copy_file "templates/uploader/controller.rb", destination_root(options[:admin_path], "/controllers/uploads.rb")
34
+ copy_file "templates/uploader/views/grid.js.erb", destination_root(options[:admin_path], "/views/uploads/grid.js.erb")
35
+ copy_file "templates/uploader/views/store.jml", destination_root(options[:admin_path], "/views/uploads/store.jml")
36
+ copy_file "templates/uploader/lib/uploader.rb", destination_root("lib", "uploader.rb")
37
+
38
+ Padrino::Generators::Model.dup.start([
39
+ "upload", "file:string", "created_at:datetime",
40
+ "-r=#{options[:root]}", "-s=#{skip_migrations}", "-d=#{options[:destroy]}"
41
+ ])
42
+
43
+ inject_into_file destination_root("app", "models", "upload.rb"), :before => "end" do
44
+ (<<-RUBY).gsub(/ {14}/, ' ')
45
+ mount_uploader :file, Uploader
46
+
47
+ def size
48
+ file.size if file
49
+ end
50
+
51
+ def content_type
52
+ file.content_type if file
53
+ end
54
+ RUBY
55
+ end
56
+
57
+ # Only for datamapper
58
+ if orm == :datamapper
59
+ inject_into_file destination_root("app", "models", "upload.rb"), :after => "property :file, String" do
60
+ ", :auto_validation => false"
61
+ end
62
+ end
63
+
64
+ add_permission(options[:admin_path], "role.project_module :uploads, \"/admin/uploads.js\"")
65
+
66
+ return if self.behavior == :revoke
67
+
68
+ say (<<-TEXT).gsub(/ {10}/,'')
69
+
70
+ -----------------------------------------------------
71
+ 1) Run migrations
72
+ 2) That's all!!
73
+ -----------------------------------------------------
74
+
75
+ TEXT
76
+ else
77
+ say "You are not at the root of a Padrino application! (config/boot.rb not found)" and return unless in_app_root?
78
+ end
79
+ end
80
+ end
81
+
82
+ end
83
+ end
@@ -9,7 +9,6 @@ class Admin < Padrino::Application
9
9
  layout false
10
10
  enable :authentication
11
11
  disable :store_location
12
- set :use_orm, <%= @orm.inspect %>
13
12
  set :login_page, "/<%= @app_path %>/sessions/new"
14
13
 
15
14
  access_control.roles_for :any do |role|
@@ -17,14 +16,13 @@ class Admin < Padrino::Application
17
16
  end
18
17
 
19
18
  access_control.roles_for :admin do |role, account|
19
+
20
20
  role.allow "/"
21
21
 
22
22
  role.project_module :accounts do |project|
23
23
  project.menu :list, "/<%= @app_path %>/accounts.js"
24
24
  project.menu :new, "/<%= @app_path %>/accounts/new"
25
25
  end
26
-
27
- # Put before other permissions [don't delete this line!!!]
28
26
  end
29
27
 
30
28
  end
@@ -32,6 +32,6 @@ Admin.controllers :accounts do
32
32
  delete :destroy, :respond_to => :json do
33
33
  accounts = Account.all(:conditions => { :id => params[:ids].split(",") })
34
34
  errors = accounts.map { |account| I18n.t("admin.general.cantDelete", :record => account.id) unless account.destroy }.compact
35
- { :success => errors.empty?, :msg => errors.join("<br />") }.to_json
35
+ render :success => errors.empty?, :msg => errors.join("<br />")
36
36
  end
37
37
  end