Sha256: 7aaab4cf7f1b9adfda0cab5c0ccdf4bfb693ea8416bf58a89e1b7b740b0ee0ee

Contents?: true

Size: 1.58 KB

Versions: 99

Compression:

Stored size: 1.58 KB

Contents

module Souls
  module Api::Generate
    ## Generate Rspec Policy
    def self.rspec_policy(class_name: "souls")
      dir_name = "./spec/policies"
      FileUtils.mkdir_p(dir_name) unless Dir.exist?(dir_name)
      file_path = "./spec/policies/#{class_name}_policy_spec.rb"
      return "RspecPolicy already exist! #{file_path}" if File.exist?(file_path)

      File.open(file_path, "w") do |new_line|
        new_line.write(<<~TEXT)
          describe #{class_name.camelize}Policy do
            subject { described_class.new(user, #{class_name.underscore}) }

            let(:#{class_name.underscore}) { FactoryBot.create(:#{class_name.underscore}) }

            context "being a visitor" do
              let(:user) { FactoryBot.create(:user, roles: :normal) }

              it { is_expected.to permit_action(:index) }
              it { is_expected.to permit_action(:show) }
              it { is_expected.to forbid_actions([:create, :update, :delete]) }
            end

            context "being a user" do
              let(:user) { FactoryBot.create(:user, roles: :user) }

              it { is_expected.to permit_actions([:create, :update]) }
            end

            context "being an admin" do
              let(:user) { FactoryBot.create(:user, roles: :admin) }

              it { is_expected.to permit_actions([:create, :update, :delete]) }
            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

99 entries across 99 versions & 1 rubygems

Version Path
souls-0.44.6 lib/souls/api/generate/rspec_policy.rb
souls-0.44.5 lib/souls/api/generate/rspec_policy.rb
souls-0.44.4 lib/souls/api/generate/rspec_policy.rb
souls-0.44.3 lib/souls/api/generate/rspec_policy.rb
souls-0.44.2 lib/souls/api/generate/rspec_policy.rb
souls-0.44.1 lib/souls/api/generate/rspec_policy.rb
souls-0.44.0 lib/souls/api/generate/rspec_policy.rb
souls-0.43.0 lib/souls/api/generate/rspec_policy.rb
souls-0.42.1 lib/souls/api/generate/rspec_policy.rb
souls-0.42.0 lib/souls/api/generate/rspec_policy.rb
souls-0.41.3 lib/souls/api/generate/rspec_policy.rb
souls-0.41.2 lib/souls/api/generate/rspec_policy.rb
souls-0.41.1 lib/souls/api/generate/rspec_policy.rb
souls-0.41.0 lib/souls/api/generate/rspec_policy.rb
souls-0.40.3 lib/souls/api/generate/rspec_policy.rb
souls-0.40.2 lib/souls/api/generate/rspec_policy.rb
souls-0.40.1 lib/souls/api/generate/rspec_policy.rb
souls-0.40.0 lib/souls/api/generate/rspec_policy.rb
souls-0.39.0 lib/souls/api/generate/rspec_policy.rb
souls-0.38.6 lib/souls/api/generate/rspec_policy.rb