Sha256: b1c91b8e093219fb31ad6984f29a5ced4719f1f0cdee8230322f8e252531fde3

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

require 'rails/generators/mongoid_generator'
require 'active_support/core_ext'

module Mongoid
  module Generators
    class RolifyGenerator < Rails::Generators::NamedBase
      source_root File.expand_path("../templates", __FILE__)

      argument :user_cname, :type => :string, :default => "User", :banner => "User"

      def generate_model
        invoke "mongoid:model", [ name ]
      end

      def inject_role_class
        inject_into_file(model_path, model_contents, :after => "include Mongoid::Document\n")
      end

      def user_reference
        user_cname.demodulize.underscore
      end

      def role_reference
        class_name.demodulize.underscore
      end

      def model_path
        File.join("app", "models", "#{file_path}.rb")
      end

      def model_contents
        content = <<RUBY
  has_and_belongs_to_many :%{user_cname}
  belongs_to :resource, :polymorphic => true

  field :name, :type => String

  index({
    :name => 1,
    :resource_type => 1,
    :resource_id => 1
  },
  { :unique => true})

  validates :resource_type,
            :inclusion => { :in => Rolify.resource_types },
            :allow_nil => true

  scopify
RUBY
        content % { :user_cname => user_cname.constantize.collection_name }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rolify-6.0.1 lib/generators/mongoid/rolify_generator.rb
rolify-6.0.0 lib/generators/mongoid/rolify_generator.rb
rolify-5.3.0 lib/generators/mongoid/rolify_generator.rb
rolify-5.2.0 lib/generators/mongoid/rolify_generator.rb
rolify-5.1.0 lib/generators/mongoid/rolify_generator.rb
rolify-5.0.0 lib/generators/mongoid/rolify_generator.rb
rolify-4.1.1 lib/generators/mongoid/rolify_generator.rb
rolify-4.1.0 lib/generators/mongoid/rolify_generator.rb
rolify-4.0.0 lib/generators/mongoid/rolify_generator.rb
rolify-3.5.2 lib/generators/mongoid/rolify_generator.rb