repositor 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57ecdac0dba23e52a2837a0a56185f999b873d90
4
- data.tar.gz: bf29c856e21fd9c84edb64114646164f350ce5d9
3
+ metadata.gz: 0e461148a09c7a400f415e1cc79503fe9a7d82f0
4
+ data.tar.gz: a5c378edf40ae7ad2eacf7c27482538ad29d3249
5
5
  SHA512:
6
- metadata.gz: ffa9d513e8a76ac0d87c87b61919d094f452445182b6c6b1e90362efe5438afc9b4adf71284e10960c0f7ad6e3dd78989525479f84699dad48f17799a7eba58e
7
- data.tar.gz: d41b29977d2bb2756f1464c82f9d799fb2d6d9ca5928b800128eb56baae17dca509ecb3acaf8ccd4a2ce32406af9d81529bfb8e297d0b8d52f48d0fa0f94d54d
6
+ metadata.gz: bef13f567c76add59904d26c9d11bd72d30cb89548d1d501af48c84cb289c2927a68132b43b45907c8e82d74b17fa4f855124bfd63f8f8083ac0114d4afc4fee
7
+ data.tar.gz: d7e3dee965a45d2b86b12e3c53efef35cd8eb68debe39691a6d415386211695a949278f07b8be1f12417200f41088dfea954c56cb182ae2c455b39d34de5845e
data/README.md CHANGED
@@ -4,76 +4,49 @@
4
4
 
5
5
  ## Installation & Setup
6
6
 
7
- In gemfile:
7
+ Manual:
8
+ ```
9
+ gem install 'repositor'
10
+ ```
11
+
12
+ For gemfile:
8
13
  ```ruby
9
14
  gem 'repositor'
10
15
  ```
11
16
 
12
- In console:
17
+ And run in console:
13
18
  ```
14
- bundle
19
+ bundle install
15
20
  ```
16
21
 
17
22
  ## Description
18
23
 
19
- This gem is an implementation of Repository Pattern described in book [Fearless Refactoring Rails Controllers](http://rails-refactoring.com/) by Andrzej Krzywda 2014 (c). Awesome book, recommend read for all who are scary open own controller files ;)
24
+ This gem is an implementation of **Repository Pattern** described in book [Fearless Refactoring Rails Controllers](http://rails-refactoring.com/) by Andrzej Krzywda 2014 (c). Awesome book, recommend read for all who are scary open own controller files ;)
20
25
 
21
- The main reason to user RepoObject is that your controller don't communicate with ORM layer (ActiveRecord or Mongoid). It must communicate with Repo layer so you are not stricted about your database adapter. If in future you will want to change it, you will need just to reconfigure your Repository layer. Sounds nice. Let's try it..
26
+ The main reason to user **RepoObject** is that your controller don't communicate with ORM layer (ActiveRecord or Mongoid). It must communicate with Repo layer so you are not stricted about your database adapter. If in future you will want to change it, you will need just to reconfigure your Repository layer. Sounds nice. Let's try it..
22
27
 
23
- With RepoObject you controller must look something like this:
28
+ With some helps of helper method your controller can be only 30-40 lines of code. Nothing more.
29
+
30
+ With RepoObject you controller could look something like this:
24
31
  ```ruby
25
32
  class ProductsController < ApplicationController
26
33
 
27
- # you are free from action callbacks...
28
-
29
- def index
30
- @products = repo.all # you communicate with repo object, not with model!
31
- end
32
-
33
- def show
34
- @product = repo.find(params[:id]) # with repo... only...
35
- end
36
-
37
- def new
38
- @product = repo.new # it's awesome! no?
39
- end
40
-
41
- def edit
42
- @product = repo.find(params[:id]) # some dry?
43
- end
34
+ # you are free from any action callbacks...
35
+ # but define this 2 helper methods:
36
+ helper_method :product, :products
44
37
 
45
38
  def create
46
- respond_to do |format|
47
- @product = repo.create(product_params) # send params to repo
48
- if @product.valid? # just check for valid, not for create action
49
- format.html { redirect_to @product, notice: 'Product was successfully created.' }
50
- format.json { render :show, status: :created, location: @product }
51
- else
52
- format.html { render :new }
53
- format.json { render json: @product.errors, status: :unprocessable_entity }
54
- end
55
- end
39
+ @product = repo.create(product_params)
40
+ @product.valid? ? redirect_to(default_redirect) : render(:new)
56
41
  end
57
42
 
58
43
  def update
59
- respond_to do |format|
60
- @product = repo.update(params[:id], product_params) # give repo id of record and params for update
61
- if @product.valid? # only validations
62
- format.html { redirect_to @product, notice: 'Product was successfully updated.' }
63
- format.json { render :show, status: :ok, location: @product }
64
- else
65
- format.html { render :edit }
66
- format.json { render json: @product.errors, status: :unprocessable_entity }
67
- end
68
- end
44
+ @product = repo.update(product, product_params)
45
+ @product.valid? ? redirect_to(default_redirect) : render(:edit)
69
46
  end
70
47
 
71
48
  def destroy
72
- repo.destroy(params[:id]) # destroy also throw repo object
73
- respond_to do |format|
74
- format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
75
- format.json { head :no_content }
76
- end
49
+ repo.destroy(product) and redirect_to default_redirect
77
50
  end
78
51
 
79
52
  private
@@ -82,13 +55,31 @@ class ProductsController < ApplicationController
82
55
  params.require(:product).permit(:name, :price, :dscription)
83
56
  end
84
57
 
58
+ def default_redirect
59
+ products_path
60
+ end
61
+
62
+ # Helper method that find or init new instance for you and cache it in ivar
63
+ # You can use it for at view show action just `product` method
64
+ # or for _form partial also `product`
65
+ def product
66
+ @product ||= repo.find(params[:id])
67
+ end
68
+
69
+ # Second helper that allow to cache all collection
70
+ # At view method `products` allows you access to the colelction
71
+ # No any @'s anymore!
72
+ def products
73
+ @products ||= repo.all
74
+ end
75
+
85
76
  # Declaration if repo object:
86
77
  def repo
87
- @products_repo ||= ProductsRepo.new
78
+ @products_repo ||= ProductRepo.new
88
79
  end
89
80
  # By default repositor will try to find `Product` model and communicate with it
90
81
  # if you need specify other model, pass in params
91
- # ProductsRepo.new(model: SaleProduct)
82
+ # ProductRepo.new(model: SaleProduct)
92
83
  end
93
84
  ```
94
85
 
@@ -106,9 +97,9 @@ class ApplicationRepo
106
97
  end
107
98
  ```
108
99
 
109
- Than you need to create `products_repo.rb`:
100
+ Than you need to create `product_repo.rb`:
110
101
  ```ruby
111
- class ProductsRepo < ApplicationRepo
102
+ class ProductRepo < ApplicationRepo
112
103
  # here you will have default methods for repo actions
113
104
  # if you want communicate with model class,
114
105
  # just can use model method to send it any method you need
@@ -123,7 +114,7 @@ If check what exactly was done, including `Repository` module in base `Applicati
123
114
 
124
115
  `Repositor` did for you a lot of dry work. In other case for each repo you must make identical methods, like this:
125
116
  ```ruby
126
- class ProductsRepo
117
+ class ProductRepo
127
118
  def all
128
119
  Product.all
129
120
  end
@@ -140,14 +131,12 @@ class ProductsRepo
140
131
  Product.create(product_params)
141
132
  end
142
133
 
143
- def update(product_id, params)
144
- find(product_id).tap do |product|
134
+ def update(params)
145
135
  product.update(params)
146
- end
147
136
  end
148
137
 
149
- def destroy(product_id)
150
- find(product_id).destroy
138
+ def destroy(product)
139
+ product.destroy
151
140
  end
152
141
  end
153
142
  ```
@@ -155,6 +144,6 @@ If you need to add new method for model, just define it in repo file.
155
144
  Keep your model skinny.
156
145
 
157
146
  ## TODO
158
- * Specs on the way...
159
147
  * Add mongoid support
148
+ * Add generators that generate repo folder and some `application_repo.rb`
160
149
  * Some improvements ? =)
@@ -24,6 +24,7 @@ module Repositor
24
24
 
25
25
  def update(record, record_params)
26
26
  record.update(record_params)
27
+ record
27
28
  end
28
29
 
29
30
  def destroy(record)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repositor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volodya Sveredyuk