Sha256: bdbcd6302ee7baafc28636704a288ecc411d5c768fcaa0267c709cb86c81faf7

Contents?: true

Size: 1.24 KB

Versions: 17

Compression:

Stored size: 1.24 KB

Contents

module Souls
  module Api::Generate
    ## Generate Policy
    def self.policy(class_name: "user")
      dir_name = "./app/policies"
      FileUtils.mkdir_p(dir_name) unless Dir.exist?(dir_name)
      file_path = "#{dir_name}/#{class_name.singularize}_policy.rb"
      return "Policy already exist! #{file_path}" if File.exist?(file_path)

      File.open(file_path, "w") do |f|
        f.write(<<~TEXT)
          class #{class_name.camelize}Policy < ApplicationPolicy
            def show?
              true
            end

            def index?
              true
            end

            def create?
              user_permissions?
            end

            def update?
              user_permissions?
            end

            def delete?
              admin_permissions?
            end

            private

            def user_permissions?
              @user.master? or @user.admin? or @user.user?
            end

            def admin_permissions?
              @user.master? or @user.admin?
            end
          end
        TEXT
      end
      puts(Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }])
      file_path
    rescue StandardError => e
      raise(StandardError, e)
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
souls-0.51.2 lib/souls/api/generate/policy.rb
souls-0.51.1 lib/souls/api/generate/policy.rb
souls-0.51.0 lib/souls/api/generate/policy.rb
souls-0.50.0 lib/souls/api/generate/policy.rb
souls-0.49.0 lib/souls/api/generate/policy.rb
souls-0.48.2 lib/souls/api/generate/policy.rb
souls-0.48.1 lib/souls/api/generate/policy.rb
souls-0.48.0 lib/souls/api/generate/policy.rb
souls-0.47.1 lib/souls/api/generate/policy.rb
souls-0.47.0 lib/souls/api/generate/policy.rb
souls-0.46.1 lib/souls/api/generate/policy.rb
souls-0.46.0 lib/souls/api/generate/policy.rb
souls-0.45.4 lib/souls/api/generate/policy.rb
souls-0.45.3 lib/souls/api/generate/policy.rb
souls-0.45.2 lib/souls/api/generate/policy.rb
souls-0.45.1 lib/souls/api/generate/policy.rb
souls-0.45.0 lib/souls/api/generate/policy.rb