Module: Moat::RSpec::PolicyExampleGroup

Includes:
PolicyMatchers
Defined in:
lib/moat/rspec.rb

Class Method Summary collapse

Methods included from PolicyMatchers

#current_role, #generate_failure_message, #permitted_authorizations, #permitted_through_filters, #role

Class Method Details

.included(base_class) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/moat/rspec.rb', line 155

def self.included(base_class)
  base_class.[:type] = :policy

  class << base_class
    def roles(*roles, &block)
      roles.each do |role|
        describe(role.to_s, role: role, caller: caller) { instance_eval(&block) }
      end
    end
    alias_method :role, :roles

    def resource(&block)
      fail ArgumentError, "#{__method__} called without a block" unless block
      let(:policy_example_resource) { instance_eval(&block) }
    end

    def scope(&block)
      fail ArgumentError, "#{__method__} called without a block" unless block
      let(:policy_example_scope) { instance_eval(&block) }
    end

    def policy_filters(*filters)
      let(:policy_filters) { filters }
    end

    def policy_authorizations(*authorizations)
      let(:policy_authorizations) { authorizations }
    end
  end

  base_class.class_eval do
    subject { described_class }

    let(:policy_authorizations) do
      fail NotImplementedError, "List of policy_authorizations undefined"
    end

    let(:policy_filters) do
      fail NotImplementedError, "List of policy_filters undefined"
    end

    let(:policy_example_resource) do
      fail NotImplementedError, "A resource has not been defined"
    end

    # a scope that contains at least the resource
    let(:policy_example_scope) do
      if policy_example_resource.class.respond_to?(:all)
        policy_example_resource.class.all
      else
        [policy_example_resource]
      end
    end
  end
end