uncouple 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa336a1a181a1dcf4fadce08625824b04de395cd
4
- data.tar.gz: 8acdaf19fcf59852b73ac91df1c479aebd789812
3
+ metadata.gz: 8b8258d3acabe57af68da9c30a1df945dc11c8fb
4
+ data.tar.gz: 93805d726a1602697a3d1e9bc879c2ddfe6d93d0
5
5
  SHA512:
6
- metadata.gz: 16d8ff285865f617df7cebd73882af6256c028b9dec0bbfd5f66995d7d3cb6fdc03c1b25daf0740bb47893f65464cf27d95340f1292e3add8f00e434959cb515
7
- data.tar.gz: 7466e07a10605deac628fac0890af012c20161631a2018eb65b60f4b5c01e3ec55e6fdb0f6f69ec3b0e1a826737a7c9c280b9d32625cdc1022776232fe14ecf6
6
+ metadata.gz: 36d8cd6f56f95227a3fde0457495a133718b42f036f4f9ab93907be6a19f1516014f622184900b49c5ccaf242b3fc1aee30032863f2d088f4207d0419799d2c0
7
+ data.tar.gz: 1ee955b4721fd90040b72eae1eaa975cceb6f4b80438b2e30d4986d37b61a16c1e7569aa3b11341a9c33603b64f47f538dd59485f062da1400a3d81545b10f36
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Uncouple
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/uncouple.svg)](https://badge.fury.io/rb/uncouple)
3
4
  [![Build Status](https://travis-ci.org/citrus/uncouple.svg?branch=master)](https://travis-ci.org/citrus/uncouple)
4
5
 
5
6
  Uncouple your business logic from Rails or whatever other framework you may be using.
@@ -100,6 +101,24 @@ class MetricsController < ApplicationController
100
101
  end
101
102
  ```
102
103
 
104
+ If available, `Uncouple::ActionPerformer` injects your controller's `current_user` into the action and calls `perform_with_authorization` under the hood. Overwrite `authorize!` in your action to ensure the user has permission to call the action.
105
+
106
+ Actions also include a `current_user` helper method.
107
+
108
+ For example, you can use pundit like this:
109
+
110
+ ```rb
111
+ class Metric::CreateAction < Uncouple::Action
112
+
113
+ def authorize!
114
+ Pundit.authorize(current_user, Metric, :create?)
115
+ end
116
+
117
+ # ...
118
+
119
+ end
120
+ ```
121
+
103
122
 
104
123
  ## Development
105
124
 
@@ -1,10 +1,14 @@
1
+ require "uncouple/action/authorization"
2
+
1
3
  module Uncouple
2
4
  class Action
3
5
 
6
+ include Uncouple::Action::Authorization
7
+
4
8
  attr_reader :params
5
9
 
6
10
  def initialize(params=nil)
7
- @params = recast_parameters(params)
11
+ @params = recast_parameters(params || {})
8
12
  end
9
13
 
10
14
  def perform
@@ -22,7 +26,6 @@ module Uncouple
22
26
  private
23
27
 
24
28
  def recast_parameters(params)
25
- return if params.nil?
26
29
  if defined?(ActionController::Parameters)
27
30
  recast_as_strong_parameters(params)
28
31
  elsif defined?(ActiveSupport::HashWithIndifferentAccess)
@@ -0,0 +1,20 @@
1
+ module Uncouple
2
+ class Action
3
+ module Authorization
4
+
5
+ def perform_with_authorization
6
+ perform if authorize!
7
+ end
8
+
9
+ def authorize!
10
+ # overwrite me!
11
+ true
12
+ end
13
+
14
+ def current_user
15
+ @current_user ||= params.delete(:current_user)
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -3,8 +3,9 @@ module Uncouple
3
3
 
4
4
  def perform(action_class, action_params=nil, &block)
5
5
  action_params ||= params if respond_to?(:params)
6
+ action_params.merge!(current_user: current_user) if respond_to?(:current_user)
6
7
  if @action = action_class.new(action_params)
7
- @action.perform
8
+ @action.perform_with_authorization
8
9
  block.call if block_given? && @action.success?
9
10
  end
10
11
  @action
@@ -1,3 +1,3 @@
1
1
  module Uncouple
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uncouple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spencer Steffen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-19 00:00:00.000000000 Z
11
+ date: 2017-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,6 +85,7 @@ files:
85
85
  - bin/setup
86
86
  - lib/uncouple.rb
87
87
  - lib/uncouple/action.rb
88
+ - lib/uncouple/action/authorization.rb
88
89
  - lib/uncouple/action_performer.rb
89
90
  - lib/uncouple/rails.rb
90
91
  - lib/uncouple/rails/generators/action_generator.rb