Sha256: 7e6b05e8fc8f29463b92e96641bfc45877e78b39d30b9bd82615a402156f7242

Contents?: true

Size: 1.61 KB

Versions: 169

Compression:

Stored size: 1.61 KB

Contents

module Souls
  class Generate < Thor
    desc "rspec_policy [CLASS_NAME]", "Generate Rspec Policy Test from schema.rb"
    def rspec_policy(class_name)
      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 Thor::Error => e
      raise(Thor::Error, e)
    end
  end
end

Version data entries

169 entries across 169 versions & 1 rubygems

Version Path
souls-1.0.3 lib/souls/cli/generate/rspec_policy.rb
souls-1.0.2 lib/souls/cli/generate/rspec_policy.rb
souls-1.0.1 lib/souls/cli/generate/rspec_policy.rb
souls-1.0.0 lib/souls/cli/generate/rspec_policy.rb
souls-0.71.7 lib/souls/cli/generate/rspec_policy.rb
souls-0.71.6 lib/souls/cli/generate/rspec_policy.rb
souls-0.71.5 lib/souls/cli/generate/rspec_policy.rb
souls-0.71.4 lib/souls/cli/generate/rspec_policy.rb
souls-0.71.3 lib/souls/cli/generate/rspec_policy.rb
souls-0.71.2 lib/souls/cli/generate/rspec_policy.rb
souls-0.71.1 lib/souls/cli/generate/rspec_policy.rb
souls-0.71.0 lib/souls/cli/generate/rspec_policy.rb
souls-0.70.8 lib/souls/cli/generate/rspec_policy.rb
souls-0.70.7 lib/souls/cli/generate/rspec_policy.rb
souls-0.70.6 lib/souls/cli/generate/rspec_policy.rb
souls-0.70.5 lib/souls/cli/generate/rspec_policy.rb
souls-0.70.4 lib/souls/cli/generate/rspec_policy.rb
souls-0.70.3 lib/souls/cli/generate/rspec_policy.rb
souls-0.70.2 lib/souls/cli/generate/rspec_policy.rb
souls-0.70.1 lib/souls/cli/generate/rspec_policy.rb