repositor 0.3.0 → 0.3.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: 0c7720d2c653d1f7eabf57f0d4030d6da5472797
4
- data.tar.gz: 130e9b1ec4a0fb5d25091c8aec2fd183e6c37971
3
+ metadata.gz: 3c3833d6329cc6b39385161b5d29bffc8fb434ec
4
+ data.tar.gz: 110ff33c7d808f0684de45f132114a3495d48e8c
5
5
  SHA512:
6
- metadata.gz: 73c039bee755096139304fc64ba3116bebfe0860b6f40a5ef7cfa5f3755694a569ca09bd89d313e4f9036d91f9f1c6c22ad6593a0cb19aae19770cbc51703202
7
- data.tar.gz: e2f1b570834ef3a20524aafc615500fc53dfc859e520b6e289d0c9bed3a40bc14376f1e6b9ab13175077f239730a865b975bd7ebb994dcffd4be091074ad9e4d
6
+ metadata.gz: 401a007577f4c87b4b86dde1f7a3ae677170c3cae3c3783d0232efe7639dd12fdac19d3967eb88560600f1774e469253ec11035e34edfb1ca3e4b29cb8990a48
7
+ data.tar.gz: ff238393c27734406b4d3cfde6c74f138f3f21bb60ae099cfa523d9119464488cf441630210c096448ca3bb59c2015b779e356e690d4641b26ad68f12817bb9d
@@ -0,0 +1,152 @@
1
+ # Repositor
2
+
3
+ ## Installation & Setup
4
+
5
+ In gemfile:
6
+ ```ruby
7
+ gem 'repositor'
8
+ ```
9
+
10
+ In console:
11
+ ```
12
+ bundle
13
+ ```
14
+
15
+ ## Description
16
+
17
+ 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 ;)
18
+
19
+ 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..
20
+
21
+ With RepoObject you controller must look something like this:
22
+ ```ruby
23
+ class ProductsController < ApplicationController
24
+
25
+ # you are free from action callbacks...
26
+
27
+ def index
28
+ @products = repo.all # you communicate with repo object, not with model!
29
+ end
30
+
31
+ def show
32
+ @product = repo.find(params[:id]) # with repo... only...
33
+ end
34
+
35
+ def new
36
+ @product = repo.new # it's awesome! no?
37
+ end
38
+
39
+ def edit
40
+ @product = repo.find(params[:id]) # some dry?
41
+ end
42
+
43
+ def create
44
+ respond_to do |format|
45
+ @product = repo.create(product_params) # send params to repo
46
+ if @product.valid? # just check for valid, not for create action
47
+ format.html { redirect_to @product, notice: 'Product was successfully created.' }
48
+ format.json { render :show, status: :created, location: @product }
49
+ else
50
+ format.html { render :new }
51
+ format.json { render json: @product.errors, status: :unprocessable_entity }
52
+ end
53
+ end
54
+ end
55
+
56
+ def update
57
+ respond_to do |format|
58
+ @product = repo.update(params[:id], product_params) # give repo id of record and params for update
59
+ if @product.valid? # only validations
60
+ format.html { redirect_to @product, notice: 'Product was successfully updated.' }
61
+ format.json { render :show, status: :ok, location: @product }
62
+ else
63
+ format.html { render :edit }
64
+ format.json { render json: @product.errors, status: :unprocessable_entity }
65
+ end
66
+ end
67
+ end
68
+
69
+ def destroy
70
+ repo.destroy(params[:id]) # destroy also throw repo object
71
+ respond_to do |format|
72
+ format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
73
+ format.json { head :no_content }
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def product_params
80
+ params.require(:product).permit(:name, :price, :dscription)
81
+ end
82
+
83
+ # Declaration if repo object:
84
+ def repo
85
+ @products_repo ||= ProductsRepo.new
86
+ end
87
+ # By default repositor will try to find `Product` model and communicate with it
88
+ # if you need specify other model, pass in params
89
+ # ProductsRepo.new(model: SaleProduct)
90
+ end
91
+ ```
92
+
93
+ ## How to use
94
+
95
+
96
+ In `app` directory you need to create new `repos` directory . Recomended to create `application_repo.rb` and inherit from it all repos, so you could keep all your repos under single point of inheritance.
97
+
98
+ ```ruby
99
+ class ApplicationRepo
100
+ # include ORM submodule
101
+ # now supported only ActiveRecord
102
+ # more will added soon
103
+ include Repositor::ActiveRecord
104
+ end
105
+ ```
106
+
107
+ Than you need to create `products_repo.rb`:
108
+ ```ruby
109
+ class ProductsRepo < ApplicationRepo;
110
+ # here you will have default methods for repo actions
111
+ end
112
+ ```
113
+ and that's all... magic already happened (no)
114
+
115
+ If check what exactly was done, including `Repository` module in base `ApplicationRepo` will add default CRUD methods to all repos that will be inherited from it. That's all. No magic.
116
+
117
+ `Repositor` did for you a lot of dry work. In other case for each repo you must make identical methods, like this:
118
+ ```ruby
119
+ class ProductsRepo
120
+ def find(product_id)
121
+ Product.find(product_id)
122
+ end
123
+
124
+ def all
125
+ Product.all
126
+ end
127
+
128
+ def new
129
+ Product.new
130
+ end
131
+
132
+ def update(product_id, params)
133
+ find(product_id).tap do |product|
134
+ product.update(params)
135
+ end
136
+ end
137
+
138
+ def destroy(product_id)
139
+ find(product_id).destroy
140
+ end
141
+
142
+ def create(product_params)
143
+ Product.create(product_params)
144
+ end
145
+ end
146
+ ```
147
+ If you need to add new method to repo, just define it in repo file.
148
+
149
+ ## TODO
150
+ * Specs on the way...
151
+ * Add mongoid support
152
+ * Some improvements ? =)
@@ -0,0 +1,35 @@
1
+ module Repositor
2
+ module ActiveRecord
3
+ attr_reader :model
4
+
5
+ def initialize(model: nil)
6
+ @model = model || self.class.to_s.chomp("Repo").singularize.constantize
7
+ end
8
+
9
+ def find(record_id)
10
+ model.send :find, record_id
11
+ end
12
+
13
+ def all
14
+ model.send :all
15
+ end
16
+
17
+ def new
18
+ model.send :new
19
+ end
20
+
21
+ def create(record_params)
22
+ model.send :create, record_params
23
+ end
24
+
25
+ def update(record_id, record_params)
26
+ find(record_id).tap do |record|
27
+ record.update(record_params)
28
+ end
29
+ end
30
+
31
+ def destroy(record_id)
32
+ find(record_id).destroy
33
+ end
34
+ end
35
+ end
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.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volodya Sveredyuk
@@ -36,7 +36,9 @@ executables: []
36
36
  extensions: []
37
37
  extra_rdoc_files: []
38
38
  files:
39
+ - README.md
39
40
  - lib/repositor.rb
41
+ - lib/repositor/active_record.rb
40
42
  homepage: https://github.com/sveredyuk/repositor
41
43
  licenses:
42
44
  - MIT