Skip to content

Commit 84114bb

Browse files
committed
Merge branch 'feature/presence' into feature/to_i-bug
* feature/presence: Changed present to presence Fixed to_i position Added category_helper.rb Changed docker-compose run position Changed elk-yml-config to elkyml-config Added elk config Changed setup pretty code # Conflicts: # app/controllers/api/v1/posts_controller.rb
2 parents 508a712 + b1488ad commit 84114bb

File tree

4 files changed

+187
-63
lines changed

4 files changed

+187
-63
lines changed

README.md

+179-55
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,32 @@
33
How to Run
44
----------
55

6+
1. Prerequisites
7+
* [Log For ELK stack (Elastic Search, Logstash, Kibana)](#log-for-elk-stack-elastic-search-logstash-kibana)
8+
* [elk.yml config](#elkyml-config)
9+
* [lograge.rb with custom config](#logragerb-with-custom-config)
10+
* [ELK Setup](/rails_log_with_elk_setup.md)
611
1. Setup
7-
1. non docker-compose
8-
1. Run Postgresql and migrate
9-
```bash
10-
bundle install
11-
````
12-
```bash
13-
rake docker:pg:init
14-
rake docker:pg:run
15-
```
16-
```bash
17-
rake db:migrate
18-
rake db:migrate RAILS_ENV=test
19-
````
20-
```bash
21-
rake db:seed
22-
```
23-
2. Run Redis for cache
24-
```bash
25-
docker run --rm --name my-redis-container -p 6379:6379 -d redis redis-server --appendonly yes
26-
redis-cli -h localhost -p 7001
27-
```
28-
3. Rails Server Run
29-
```bash
30-
rails s
31-
```
32-
2. docker-compose
12+
> You can run with ```docker-compose``` or non docker-compose
13+
1. docker-compose
14+
> server run
15+
````
16+
docker-compose up --build -d
17+
````
18+
> db setup
3319
````bash
34-
docker-compose up --build
3520
docker-compose run web bundle exec rake db:test:load
3621
docker-compose run web bundle exec rake db:migrate
3722
docker-compose run web bundle exec rake db:seed
3823
````
24+
> Testing
25+
```bash
26+
docker-compose run --no-deps web bundle exec rspec --format documentation
27+
```
28+
> Rswag for documentation ```http://localhost:3000/api-docs/index.html```
29+
```bash
30+
docker-compose run --no-deps web bundle exec rake rswag
31+
```
3932
> rails console
4033
```bash
4134
docker-compose exec web bin/rails c
@@ -44,28 +37,39 @@ How to Run
4437
```bash
4538
docker-compose run --no-deps web bundle exec rake routes
4639
```
47-
> rswag and rspec testing
40+
2. non docker-compose
41+
> bundle
4842
```bash
49-
docker-compose run --no-deps web bundle exec rake rswag
50-
docker-compose run --no-deps web bundle exec rspec --format documentation
43+
bundle install
44+
```
45+
> postgresql run
46+
```bash
47+
rake docker:pg:init
48+
rake docker:pg:run
49+
```
50+
> migrate
51+
```bash
52+
rake db:migrate RAILS_ENV=test
53+
rake db:migrate
54+
rake db:seed
55+
```
56+
> redis run
57+
```bash
58+
docker run --rm --name my-redis-container -p 6379:6379 -d redis redis-server --appendonly yes
59+
redis-cli -h localhost -p 7001
60+
```
61+
> server run
62+
```bash
63+
rails s
64+
```
65+
> Testing
66+
```bash
67+
bundle exec rpsec --format documentation
68+
```
69+
> Rswag for documentation ```http://localhost:3000/api-docs/index.html```
70+
```bash
71+
rake rswag
5172
```
52-
2. ELK
53-
```bash
54-
git clone https://github.com/deviantony/docker-elk
55-
cd docker-elk
56-
docker-compose exec -T elasticsearch bin/elasticsearch-setup-passwords auto --batch
57-
# https://github.com/deviantony/docker-elk#setting-up-user-authentication
58-
docker-compose up --build
59-
```
60-
3. Testing
61-
```bash
62-
bundle exec rpsec --format documentation
63-
```
64-
4. Swagger - Restful Api Documentation
65-
```bash
66-
rake rswag
67-
```
68-
> http://localhost:3000/api-docs/index.html
6973
7074
TODO
7175
----
@@ -117,6 +121,7 @@ How what to do
117121
* [rswag for API Documentation](#rswag-for-api-documentation)
118122
* [Codegen](#codegen)
119123
* [Log For ELK stack (Elastic Search, Logstash, Kibana)](#log-for-elk-stack-elastic-search-logstash-kibana)
124+
* [elk.yml config](#elkyml-config)
120125
* [lograge.rb with custom config](#logragerb-with-custom-config)
121126
* [ELK Setup](/rails_log_with_elk_setup.md)
122127
* [Redis](#redis)
@@ -170,6 +175,100 @@ How what to do
170175
# config/initializers/active_model_serializer.rb
171176
ActiveModelSerializers.config.default_includes = '**'
172177
```
178+
5. Pagination with serializer
179+
180+
[/app/helpers/category_helper.rb](/app/helpers/category_helper.rb)
181+
182+
```ruby
183+
# app/helpers/category_helper.rb
184+
module CategoryHelper
185+
def fetch_categories pagaination_param
186+
page = pagaination_param[:category_page]
187+
per = pagaination_param[:category_per]
188+
key = "categories"+pagaination_param.to_s
189+
categories = $redis.get(key)
190+
if categories.nil?
191+
@categories = Category.published.by_date.page(page).per(per)
192+
categories = Pagination.build_json(@categories, pagaination_param).to_json
193+
$redis.set(key, categories)
194+
$redis.expire(key, 1.hour.to_i)
195+
end
196+
categories
197+
end
198+
def clear_cache
199+
keys = $redis.keys "*categories*"
200+
keys.each {|key| $redis.del key}
201+
end
202+
end
203+
204+
class CategoriesController < ApplicationController
205+
include CategoryHelper
206+
//... your code
207+
208+
# GET /categories
209+
def index
210+
page = params[:page].presence || 1
211+
per = params[:per].presence || Pagination.per
212+
pagaination_param = {
213+
category_page: page,
214+
category_per: per,
215+
post_page: @post_page,
216+
post_per: @post_per
217+
}
218+
@categories = fetch_categories pagaination_param
219+
render json: @categories
220+
end
221+
```
222+
```ruby
223+
class Category < ApplicationRecord
224+
include CategoryHelper
225+
belongs_to :user
226+
has_many :posts
227+
scope :published, -> { where(published: true) }
228+
scope :by_date, -> { order('created_at DESC, id DESC') }
229+
validates :title, presence: true
230+
validates :body, presence: true
231+
after_save :clear_cache
232+
end
233+
```
234+
```ruby
235+
class CategorySerializer < ActiveModel::Serializer
236+
attributes :id, :title, :body, :posts_pagination
237+
has_one :user
238+
has_many :posts
239+
def posts
240+
post_page = (instance_options.dig(:pagaination_param, :post_page).presence || 1).to_i
241+
post_per = (instance_options.dig(:pagaination_param, :post_per).presence || 0).to_i
242+
object.posts.published.by_date.page(post_page).per(post_per)
243+
end
244+
def posts_pagination
245+
post_per = (instance_options.dig(:pagaination_param, :post_per).presence || Pagination.per).to_i
246+
Pagination.build_json(posts)[:posts_pagination] if post_per > 0
247+
end
248+
end
249+
```
250+
```ruby
251+
# /lib/pagination.rb
252+
class Pagination
253+
def self.information array
254+
{
255+
current_page: array.current_page,
256+
next_page: array.next_page,
257+
prev_page: array.prev_page,
258+
total_pages: array.total_pages,
259+
total_count: array.total_count
260+
}
261+
end
262+
def self.build_json array, pagaination_param = {}
263+
ob_name = array.name.downcase.pluralize.to_sym
264+
json = Hash.new
265+
json[ob_name] = ActiveModelSerializers::SerializableResource.new(array.to_a, pagaination_param: pagaination_param)
266+
pagination_name = "#{ob_name}_pagination".to_sym
267+
json[pagination_name] = self.information array
268+
json
269+
end
270+
end
271+
```
173272
174273
### Nested Model
175274
1. Comment Controller
@@ -327,20 +426,45 @@ How what to do
327426
![swagger_screencapture](/localhost-3000-api-docs-index-html.png)
328427
329428
### Codegen
330-
> We developed server side code and We shoud need Client code. you can use Swagger-Codegen https://github.com/swagger-api/swagger-codegen#swagger-code-generator
331-
```bash
332-
brew install swagger-codegen
333-
swagger-codegen generate -i http://localhost:3000/api-docs/v1/swagger.yaml -l swift5 -o ./swift
334-
```
429+
> We developed server side code and We shoud need Client code. you can use Swagger-Codegen https://github.com/swagger-api/swagger-codegen#swagger-code-generator
430+
431+
```bash
432+
brew install swagger-codegen
433+
swagger-codegen generate -i http://localhost:3000/api-docs/v1/swagger.yaml -l swift5 -o ./swift
434+
```
335435
336436
### Log For ELK stack (Elastic Search, Logstash, Kibana)
337437
438+
#### elk.yml config
439+
> enable value is ```false``` or ```true``` <br/>
440+
exmaple : ```enable: false```<br/>
441+
[elk.yml](/config/elk.yml) <br/>
442+
443+
```yml
444+
# config/elk.yml
445+
446+
default: &default
447+
enable: false
448+
protocal: udp
449+
host: localhost
450+
port: 5000
451+
452+
development:
453+
<<: *default
454+
455+
test:
456+
<<: *default
457+
458+
production:
459+
<<: *default
460+
```
461+
338462
#### lograge.rb with custom config
339463
https://github.com/roidrage/lograge
340464
https://ericlondon.com/2017/01/26/integrate-rails-logs-with-elasticsearch-logstash-kibana-in-docker-compose.html
341465
[lograge.rb](/config/lograge.rb) <br/>
342-
[elk.yml](/config/elk.yml) <br/>
343466
[application.rb](/config/application.rb) https://guides.rubyonrails.org/v4.2/configuring.html#custom-configuration
467+
344468
> override append_info_to_payload for lograge, append_info_to_payload method put parameter to payload[]
345469
```ruby
346470
class ApplicationController < ActionController::API
@@ -397,8 +521,8 @@ $redis = Redis::Namespace.new("tutorial_post", :redis => Redis.new(:host => '127
397521
```ruby
398522
# GET /categories
399523
def index
400-
page = params[:page].present? ? params[:page] : 1
401-
per = params[:per].present? ? params[:per] : 10
524+
page = params[:page].presence || 1
525+
per = params[:per].presence || Pagination.per
402526
pagaination_param = {
403527
category_page: page,
404528
category_per: per,

app/controllers/api/v1/categories_controller.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class CategoriesController < ApplicationController
1414

1515
# GET /categories
1616
def index
17-
page = params[:page].present? ? params[:page] : 1
18-
per = params[:per].present? ? params[:per] : 10
17+
page = params[:page].presence || 1
18+
per = params[:per].presence || Pagination.per
1919
pagaination_param = {
2020
category_page: page,
2121
category_per: per,
@@ -73,8 +73,8 @@ def category_params
7373
end
7474

7575
def post_pagination_params
76-
@post_page = params[:post_page].present? ? params[:post_page] : 1
77-
@post_per = params[:post_per].present? ? params[:post_per] : 10
76+
@post_page = params[:post_page].presence || 1
77+
@post_per = params[:post_per].presence || Pagination.per
7878
end
7979
end
8080
end

app/controllers/api/v1/comments_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class CommentsController < ApplicationController
1010
# GET /comments
1111
def index
1212
post_id = params[:post_id]
13-
page = params[:page].present? ? params[:page] : 1
14-
per = params[:per].present? ? params[:per] : 10
13+
page = params[:page].presence || 1
14+
per = params[:per].presence || Pagination.per
1515
@post = Post.published.find(post_id) if post_id.present?
1616
category_id = @post.category_id
1717
@category = Category.published.find(category_id)

app/controllers/api/v1/posts_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class PostsController < ApplicationController
1313
def index
1414
category_id = params[:category_id]
1515
search = params[:search]
16-
page = params[:page].present? ? params[:page] : 1
17-
per = params[:per].present? ? params[:per] : Pagination.per
16+
page = params[:page].presence || 1
17+
per = params[:per].presence || Pagination.per
1818
@category = Category.published.find(category_id) if category_id.present?
1919
pagaination_param = {
2020
category_id: category_id,

0 commit comments

Comments
 (0)