redress 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +73 -10
- data/lib/redress/identity.rb +1 -1
- data/lib/redress/utils/parse_attributes_from_params.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41402aaf89ea4b30ca985c6748259e94f43a3bf0
|
4
|
+
data.tar.gz: f38303010581b1cc238927768afe86e48bbab4ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 372b234a3fc2d5723938e4486aa1cde2876013c67b6e3d2195ed89ead3928c6504a724262cdcb81e4149dc495d1c36b632d4018483010f8ecdb8ed20d34e2518
|
7
|
+
data.tar.gz: 4f51c86618c0e981532cdb4728a9910d8692a0011d77397c7ff316bc35091e09249cf362c18c5229b2c6125e7e484950b1983da220c3969b7539bbf164fc03e3
|
data/README.md
CHANGED
@@ -6,16 +6,14 @@
|
|
6
6
|
[](https://coveralls.io/github/galetahub/redress?branch=master)
|
7
7
|
[](https://gemnasium.com/github.com/galetahub/redress)
|
8
8
|
|
9
|
-
## Motivation
|
9
|
+
## Motivation (Command pattern)
|
10
10
|
|
11
|
-
|
11
|
+
The command pattern is sometimes called a service object, an operation, an action, and probably more names that I’m not aware of. Whatever the name we gave it, the purpose of such a pattern is rather simple: take a business action and put it behind an object with a simple interface.
|
12
12
|
|
13
13
|
<!-- Tocer[start]: Auto-generated, don't remove. -->
|
14
14
|
|
15
15
|
## Table of Contents
|
16
16
|
|
17
|
-
- [Features](#features)
|
18
|
-
- [Screencasts](#screencasts)
|
19
17
|
- [Requirements](#requirements)
|
20
18
|
- [Setup](#setup)
|
21
19
|
- [Usage](#usage)
|
@@ -29,13 +27,13 @@ TODO:
|
|
29
27
|
|
30
28
|
<!-- Tocer[finish]: Auto-generated, don't remove. -->
|
31
29
|
|
32
|
-
## Features
|
33
|
-
|
34
|
-
TODO:
|
35
|
-
|
36
30
|
## Requirements
|
37
31
|
|
38
32
|
0. [Ruby 2.3](https://www.ruby-lang.org)
|
33
|
+
1. wisper
|
34
|
+
2. fast_attributes
|
35
|
+
3. hashie
|
36
|
+
4. activemodel
|
39
37
|
|
40
38
|
## Setup
|
41
39
|
|
@@ -59,11 +57,32 @@ class ApplicationForm < Redress::Form
|
|
59
57
|
end
|
60
58
|
```
|
61
59
|
|
60
|
+
Let's define simple form:
|
61
|
+
|
62
|
+
``` ruby
|
63
|
+
class SimpleForm < ApplicationForm
|
64
|
+
mimic :user
|
65
|
+
|
66
|
+
schema do
|
67
|
+
attribute :name, String
|
68
|
+
attribute :email, String
|
69
|
+
attribute :name_with_email, String
|
70
|
+
end
|
71
|
+
|
72
|
+
validates :name, presence: true
|
73
|
+
validates :email, presence: true
|
74
|
+
|
75
|
+
def map_model(user)
|
76
|
+
self.name_with_email = "#{user.name} <#{user.email}>"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
62
81
|
### Commands
|
63
82
|
|
64
83
|
``` ruby
|
65
84
|
# app/commands/application_command.rb
|
66
|
-
require 'redress/
|
85
|
+
require 'redress/command'
|
67
86
|
|
68
87
|
class ApplicationCommand < Redress::Commad
|
69
88
|
end
|
@@ -73,7 +92,7 @@ Or if you are using ActiveRecord:
|
|
73
92
|
|
74
93
|
``` ruby
|
75
94
|
# app/commands/application_command.rb
|
76
|
-
require 'redress/
|
95
|
+
require 'redress/command'
|
77
96
|
|
78
97
|
class ApplicationCommand < Redress::Commad
|
79
98
|
def transaction(&block)
|
@@ -82,6 +101,50 @@ class ApplicationCommand < Redress::Commad
|
|
82
101
|
end
|
83
102
|
```
|
84
103
|
|
104
|
+
Simple command for user registration:
|
105
|
+
|
106
|
+
``` ruby
|
107
|
+
# app/commands/users/create_command.rb
|
108
|
+
module Users
|
109
|
+
class CreateCommand < ApplicationCommand
|
110
|
+
def initialize(form)
|
111
|
+
@form = form
|
112
|
+
end
|
113
|
+
|
114
|
+
def call
|
115
|
+
return broadcast(:invalid, @form) if @form.invalid?
|
116
|
+
|
117
|
+
user = User.create(@form.attributes)
|
118
|
+
|
119
|
+
broadcast(:ok, user)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
### Controllers
|
126
|
+
|
127
|
+
``` ruby
|
128
|
+
# app/controllers/users_controller.rb
|
129
|
+
class UsersController < Account::BaseController
|
130
|
+
respond_to :json, only: :update
|
131
|
+
|
132
|
+
def new
|
133
|
+
@user_form = SimpleForm.new
|
134
|
+
end
|
135
|
+
|
136
|
+
def create
|
137
|
+
@user_form = SimpleForm.from_params(params)
|
138
|
+
|
139
|
+
Users::CreateCommand.call(@user_form) do |c|
|
140
|
+
c.on(:ok) { head status: 201 }
|
141
|
+
c.on(:invalid) { |form| render status: 422, json: { errors: form.errors } }
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
```
|
147
|
+
|
85
148
|
## Tests
|
86
149
|
|
87
150
|
To test, run:
|
data/lib/redress/identity.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redress
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Galeta
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: wisper
|