Class: Padrino::Generators::BsAdmin

Inherits:
Thor::Group
  • Object
show all
Includes:
Actions, Admin::Actions, Thor::Actions
Defined in:
lib/bootstrap-on/bs_admin.rb

Overview

Defines the generator for creating a new admin app.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

Defines the “banner” text for the CLI.



14
# File 'lib/bootstrap-on/bs_admin.rb', line 14

def self.banner; "padrino g bs_admin"; end

.source_rootObject

Define the source template root and themes.



12
# File 'lib/bootstrap-on/bs_admin.rb', line 12

def self.source_root; File.expand_path(File.dirname(__FILE__)); end

Instance Method Details

#create_adminObject

Copies over the Padrino base admin application



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bootstrap-on/bs_admin.rb', line 31

def create_admin
  self.destination_root = options[:root]
  if in_app_root?
    unless supported_orm.include?(orm)
      say "<= At the moment, Padrino only supports #{supported_orm.join(" or ")}. Sorry!", :yellow
      raise SystemExit
    end

    # unless self.class.themes.include?(options[:theme])
    #   say "<= You need to choose a theme from: #{self.class.themes.join(", ")}", :yellow
    #   raise SystemExit
    # end

    tmp_ext = options[:renderer] || fetch_component_choice(:renderer)
    unless supported_ext.include?(tmp_ext.to_sym)
      say "<= Your are using '#{tmp_ext}' and for the admin we only support '#{supported_ext.join(', ')}'. Please use -e haml or -e erb or -e slim", :yellow
      raise SystemExit
    end

    store_component_choice(:admin_renderer, tmp_ext)

    self.behavior = :revoke if options[:destroy]

    empty_directory destination_root("admin")

    # Setup Admin Model
    @model_name     = options[:admin_model].classify
    @model_singular = @model_name.underscore
    @model_plural   = @model_singular.pluralize

    directory "templates/app", destination_root("admin")
    directory "templates/assets", destination_root("public", "admin")
    template  "templates/app.rb.tt", destination_root("admin/app.rb")
    append_file destination_root("config/apps.rb"),  "\nPadrino.mount(\"Admin\").to(\"/admin\")"
    insert_middleware 'ActiveRecord::ConnectionAdapters::ConnectionManagement', 'admin' if [:mini_record, :activerecord].include?(orm)

    params = [
      @model_singular, "name:string", "surname:string", "email:string", "crypted_password:string", "role:string",
      "-a=#{options[:app]}",
      "-r=#{options[:root]}"
    ]
    params << "-s" if options[:skip_migration]
    params << "-d" if options[:destroy]

    Padrino::Generators::Model.start(params)
    column = Struct.new(:name, :type)
    columns = [:id, :name, :surname, :email].map { |col| column.new(col) }
    column_fields = [
      { :name => :name,                  :field_type => :text_field },
      { :name => :surname,               :field_type => :text_field },
      { :name => :email,                 :field_type => :text_field },
      { :name => :password,              :field_type => :password_field },
      { :name => :password_confirmation, :field_type => :password_field },
      { :name => :role,                  :field_type => :text_field }
    ]

    admin_app = Padrino::Generators::BsAdminPage.new([@model_singular], :root => options[:root], :destroy => options[:destroy])
    admin_app.default_orm = Padrino::Admin::Generators::Orm.new(@model_singular, orm, columns, column_fields)
    admin_app.invoke_all

    template "templates/account/#{orm}.rb.tt", destination_root(options[:app], "models", "#{@model_singular}.rb"), :force => true

    if File.exist?(destination_root("db/seeds.rb"))
      run "mv #{destination_root('db/seeds.rb')} #{destination_root('db/seeds.old')}"
    end
    template "templates/account/seeds.rb.tt", destination_root("db/seeds.rb")

    empty_directory destination_root("admin/controllers")
    empty_directory destination_root("admin/views")
    empty_directory destination_root("admin/views/base")
    empty_directory destination_root("admin/views/layouts")
    empty_directory destination_root("admin/views/sessions")

    template "templates/#{ext}/app/base/index.#{ext}.tt",          destination_root("admin/views/base/index.#{ext}")
    template "templates/#{ext}/app/layouts/application.#{ext}.tt", destination_root("admin/views/layouts/application.#{ext}")
    template "templates/#{ext}/app/sessions/new.#{ext}.tt",        destination_root("admin/views/sessions/new.#{ext}")

    add_project_module @model_plural
    require_dependencies('bcrypt-ruby', :require => 'bcrypt')

    # A nicer select box
    gsub_file destination_root("admin/views/#{@model_plural}/_form.#{ext}"), "f.text_field :role, :class => :text_field", "f.select :role, :options => access_control.roles"

    # Destroy account only if not logged in
    gsub_file destination_root("admin/controllers/#{@model_plural}.rb"), "if #{@model_singular}.destroy", "if #{@model_singular} != current_account && #{@model_singular}.destroy"
    return if self.behavior == :revoke

    instructions = []
    instructions << "Run 'bundle install'"
    instructions << "Run 'padrino rake ar:migrate'"
    instructions << "Run 'padrino rake seed'"
    instructions << "Visit the admin panel in the browser at '/admin'"
    instructions.map! { |i| "  #{instructions.index(i)+1}) #{i}" }

    say
    say "="*65, :green
    say "The admin panel has been mounted! Next, follow these steps:", :green
    say "="*65, :green
    say instructions.join("\n")
    say "="*65, :green
    say
  else
    say "You are not at the root of a Padrino application! (config/boot.rb not found)"
  end
end