Sha256: d6052f5d7cf5113bed6e89f035b39117c6f56ef9a5de1190b07f75096ea55d4e

Contents?: true

Size: 1.23 KB

Versions: 74

Compression:

Stored size: 1.23 KB

Contents

module Souls
  module Generate
    ## Generate Policy
    def self.policy(class_name: "souls")
      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

74 entries across 74 versions & 1 rubygems

Version Path
souls-0.28.12 lib/souls/generate/policy.rb
souls-0.28.11 lib/souls/generate/policy.rb
souls-0.28.10 lib/souls/generate/policy.rb
souls-0.28.9 lib/souls/generate/policy.rb
souls-0.28.8 lib/souls/generate/policy.rb
souls-0.28.7 lib/souls/generate/policy.rb
souls-0.28.6 lib/souls/generate/policy.rb
souls-0.28.5 lib/souls/generate/policy.rb
souls-0.28.4 lib/souls/generate/policy.rb
souls-0.28.3 lib/souls/generate/policy.rb
souls-0.28.2 lib/souls/generate/policy.rb
souls-0.28.1 lib/souls/generate/policy.rb
souls-0.28.0 lib/souls/generate/policy.rb
souls-0.27.8 lib/souls/generate/policy.rb
souls-0.27.7 lib/souls/generate/policy.rb
souls-0.27.6 lib/souls/generate/policy.rb
souls-0.27.5 lib/souls/generate/policy.rb
souls-0.27.4 lib/souls/generate/policy.rb
souls-0.27.3 lib/souls/generate/policy.rb
souls-0.27.2 lib/souls/generate/policy.rb