Sha256: 02b4fc859f791b5c1538992f35d8f37c80327db62c72af1fbc340d04d6c838e7

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

require 'rails3_artifactor'

module RolesGeneric 
  module Generators
    class RolesGenerator < Rails::Generators::NamedBase
      include Rails3::Assist::Artifact::Model
      
      desc "Add role strategy to a model" 
      
      class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string', 
                   :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, one_role, many_roles, roles_mask)"


      class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"

      # hook_for :orm
            
      def apply_role_strategy
        insert_into_model name do
          insertion_text
        end
      end 
      
      protected                  

      def default_roles
        [:admin, :guest]        
      end

      def roles_to_add
        default_roles.concat(options[:roles]).compact.uniq
      end

      def roles
        @roles ||= roles_to_add.map{|r| ":#{r}" }
      end

      def role_strategy_statement 
        "strategy :#{strategy}\n"
      end

      def role_class_statement
        [:one_role, :many_roles, :embed_one_role, :embed_many_roles].include?(strategy.to_sym) ? 'role_class :role' : ''
      end

      def roles_statement
        roles ? "valid_roles_are #{roles.join(',')}" : ''
      end

      def insertion_text
        %Q{  
  include Roles::#{orm.to_s.camelize} 
  #{role_strategy_statement}
  #{role_class_statement}
  #{roles_statement}
}
      end

      def strategy
        options[:strategy]                
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
roles_generic-0.3.7.2 lib/generators/roles_generic/roles/roles_generator.rb
roles_generic-0.3.7.1 lib/generators/roles_generic/roles/roles_generator.rb
roles_generic-0.3.7 lib/generators/roles_generic/roles/roles_generator.rb
roles_generic-0.3.6 lib/generators/roles_generic/roles/roles_generator.rb
roles_generic-0.3.5 lib/generators/roles_generic/roles/roles_generator.rb
roles_generic-0.3.4 lib/generators/roles_generic/roles/roles_generator.rb