Sha256: 9d6ac0491199d355a7c3d1517fd066a5aeac2263933f23cf42400aad1d89d8c4

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

class TheRoleGenerator < Rails::Generators::NamedBase
  source_root File.expand_path('../templates', __FILE__)
  # argument :xname, type: :string, default: :xname

  def generate_controllers
    if gen_name == 'install'
      cp_setup
      cp_models
    elsif gen_name == 'setup'
      cp_setup
    elsif gen_name == 'models'
      cp_models
    elsif gen_name == 'admin'
      create_admin_role
    else
      puts 'TheComments Generator - wrong Name'
      puts 'Try to use install'
    end
  end

  private

  def gen_name
    name.to_s.downcase
  end

  def cp_models
    copy_file 'role.rb', 'app/models/role.rb'
  end

  def cp_setup
    copy_file 'the_role.rb', 'config/initializers/the_role.rb'
  end

  def create_admin_role
    puts `clear`
    puts '~'*40
    puts 'TheRole'
    puts '~'*40

    unless Role.with_name(:admin)
      role = Role.create(
        name: :admin,
        title: "role for admin",
        description:"this user can do anything"
      )

      role.create_rule(:system, :administrator)
      role.rule_on(:system, :administrator)

      puts "Admin role created"
    else
      puts "Admin role exists"
    end

    puts "Now you can makes any user as Admin:"
    puts "> bin/rails c"
    puts "> User.first.update( role: Role.with_name(:admin) )"
    puts '~'*40
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
the_role-2.1.1 lib/generators/the_role/the_role_generator.rb
the_role-2.1.0 lib/generators/the_role/the_role_generator.rb
the_role-2.0.4 lib/generators/the_role/the_role_generator.rb
the_role-2.0.3 lib/generators/the_role/the_role_generator.rb
the_role-2.0.2 lib/generators/the_role/the_role_generator.rb