Sha256: 2cde17d61f9d6b9ad9c51f41c659cb26e0577cf8da86721a55eb36881bbf3fe5

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

require 'rails/generators/base'
require 'sugar-high/array'
require 'active_support/inflector'
require 'rails_artifactor'
# require 'logging_assist'

module Cancan
  module Generators
    class LicenseGenerator < Rails::Generators::Base
      desc "Creates a License for a Permit in 'app/licenses' with reusable permissions"

      argument     :name,         :type => :string,    :default => '',  :desc => "Name of license"

      class_option :creates,      :type => :array,     :default => [],  :desc => "Models allowed to create"
      class_option :manages,      :type => :array,     :default => [],  :desc => "Models allowed to manage"
      class_option :reads,        :type => :array,     :default => [],  :desc => "Models allowed to read"
      class_option :owns,         :type => :array,     :default => [],  :desc => "Models allowed to own"

      class_option :licenses,     :type => :array,     :default => [],  :desc => "Sub licenses"

      source_root File.dirname(__FILE__) + '/templates'

      def main_flow      
        return if name.empty?
        create_license
      end
  
      protected

      # include Rails3::Assist::BasicLogger

      def creates
        options[:creates]
      end

      def manages
        options[:manages]
      end

      def licenses
        options[:licenses]
      end

      def reads
        options[:reads]
      end

      def owns
        options[:owns]
      end

      def create_license        
        template "license.rb", "app/licenses/#{name}_license.rb"
      end 

      def enforce_logic
        [:creates, :manages, :owns, :reads].map do |a|
          options[a].map do |c| 
            "can(:#{a.to_s.singularize}, #{act_model(c)})"
          end.join("\n    ")
        end.join("\n    ")        
      end

      def act_model name
        return ':all' if name == 'all'
        name.camelize
      end

      def license_logic
        return '' if licenses.empty?
        ls = licenses.map{|c| ":#{c}"}.join(", ")
        "licenses #{ls}"
      end  
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cancan-permits-0.3.11 lib/generators/cancan/license/license_generator.rb