Skip to content

Commit 2a9309d

Browse files
committed
Merge branch 'feature/active-storage' into develop
2 parents 6a99e91 + 1c239a0 commit 2a9309d

File tree

13 files changed

+160
-27
lines changed

13 files changed

+160
-27
lines changed

README.md

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ How to Run
1010
bundle install
1111
````
1212
```bash
13-
rake docker:db:init
14-
rake docker:db:run
13+
rake docker:pg:init
14+
rake docker:pg:run
1515
```
1616
```bash
1717
rake db:migrate
@@ -22,7 +22,7 @@ How to Run
2222
```
2323
2. Run Redis for cache
2424
```bash
25-
docker run --rm --name my-redis-container -p 7001:6379 -d redis redis-server --appendonly yes
25+
docker run --rm --name my-redis-container -p 6379:6379 -d redis redis-server --appendonly yes
2626
redis-cli -h localhost -p 7001
2727
```
2828
3. Rails Server Run
@@ -35,9 +35,20 @@ How to Run
3535
docker-compose run web bundle exec rake db:test:load
3636
docker-compose run web bundle exec rake db:migrate
3737
docker-compose run web bundle exec rake db:seed
38-
docker-compose run web bundle exec rake rswag
39-
docker-compose run web bundle exec rake spec --format documentation
4038
````
39+
> rails console
40+
```bash
41+
docker-compose exec web bin/rails c
42+
```
43+
> routes
44+
```bash
45+
docker-compose run --no-deps web bundle exec rake routes
46+
```
47+
> rswag and rspec testing
48+
```bash
49+
docker-compose run --no-deps web bundle exec rake rswag
50+
docker-compose run --no-deps web bundle exec rspec --format documentation
51+
```
4152
2. ELK
4253
```bash
4354
git clone https://github.com/deviantony/docker-elk
@@ -88,7 +99,7 @@ TODO
8899
- [ ] model tracking https://github.com/paper-trail-gem/paper_trail
89100
- [x] ELK https://github.com/deviantony/docker-elk
90101
- [x] Versioning http://railscasts.com/episodes/350-rest-api-versioning?view=asciicast
91-
- [ ] File upload to Local path with active storage https://edgeguides.rubyonrails.org/active_storage_overview.html
102+
- [x] File upload to Local path with active storage https://edgeguides.rubyonrails.org/active_storage_overview.html https://edgeguides.rubyonrails.org/active_storage_overview.html#has-many-attached
92103
- [ ] docker-compose
93104
- [ ] staging
94105
- [ ] production
@@ -111,8 +122,10 @@ How what to do
111122
* [Redis](#redis)
112123
* [server run](#server-run)
113124
* [add gem](#add-gem)
114-
* [config](#config)
125+
* [config](#config)
115126
* [how to added cache](#how-to-added-cache)
127+
* [Active Storage](#active-storage)
128+
* [Setup](#setup)
116129

117130
### Build Json with active_model_serializers Gem
118131
1. Gemfile
@@ -267,6 +280,16 @@ How what to do
267280
curl -X POST -i http://localhost:3000/posts -d '{"post": {"body":"hihihi ahaha"}}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE1Nzc0OTAyNjJ9.PCY7kXIlImORySIeDd78gErhqApAyGP6aNFBmK_mdXY"
268281
curl -X POST -i http://localhost:3000/posts -d '{"post": {"body":"Average Speed Time Time Time Current"}}' -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoyLCJleHAiOjE1Nzc0OTMwMjl9.s9WqkyM84LQGZUtpmfmZzWN8rsVUp4_yfKfxEN_t4AQ"
269282
```
283+
284+
> file upload
285+
```bash
286+
curl -H "Authorization: eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE1ODExOTA3NTV9.oaPeMu1hoinllzFGKb_7frFPwdyYzbR0wc93GOMBTeI" \
287+
-F "post[body]=string123" \
288+
-F "post[category_id]=1" \
289+
-F "post[files][]=@/Users/rhee/Desktop/item/log/47310817701116.csv" \
290+
-F "post[files][]=@/Users/rhee/Desktop/item/log/47310817701116.csv" \
291+
-X POST http://localhost:3000/api/v1/posts
292+
```
270293
4. Index Post
271294
```bash
272295
curl -X GET http://localhost:3000/posts -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE1Nzc0OTAyNjJ9.PCY7kXIlImORySIeDd78gErhqApAyGP6aNFBmK_mdXY" | jq
@@ -416,4 +439,12 @@ $redis = Redis::Namespace.new("tutorial_post", :redis => Redis.new(:host => '127
416439
keys.each {|key| $redis.del key}
417440
end
418441
end
419-
```
442+
```
443+
444+
445+
### Active Storage
446+
#### Setup
447+
```bash
448+
rails active_storage:install
449+
rake db:migrate
450+
```

app/controllers/api/v1/posts_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def set_category category_id
8282

8383
# Only allow a trusted parameter "white list" through.
8484
def post_params
85-
params.require(:post).permit(:body, :category_id).merge(user_id: @current_user.id)
85+
params.require(:post).permit(:body, :category_id, files: []).merge(user_id: @current_user.id)
8686
end
8787
end
8888
end

app/models/post.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Post < ApplicationRecord
44
belongs_to :category
55
belongs_to :user
66
has_many :comments
7+
has_many_attached :files
78
scope :category, lambda{ |category_id| where(category_id: category_id) if category_id.present? }
89
scope :search, lambda{ |search| self.where("body LIKE ?", "%#{search}%") if search.present? }
910
scope :published, -> { where(published: true) }

app/serializers/post_serializer.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
class PostSerializer < ActiveModel::Serializer
2-
attributes :id, :body, :comments_pagination
2+
include Rails.application.routes.url_helpers
3+
attributes :id, :body, :files, :comments_pagination
34
has_one :user
45
has_many :comments
6+
def files
7+
return unless object.files.attachments
8+
file_urls = object.files.map do |file|
9+
rails_blob_url(file)
10+
end
11+
file_urls
12+
end
513
def comments
614
comment_page = instance_options.dig(:pagaination_param, :comment_page).presence.to_i || 1
715
comment_per = instance_options.dig(:pagaination_param, :comment_per).presence.to_i || 0

config/environments/development.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@
4949
# Use an evented file watcher to asynchronously detect changes in source code,
5050
# routes, locales, etc. This feature depends on the listen gem.
5151
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
52+
53+
routes.default_url_options[:host] = 'localhost:3000'
5254
end

config/environments/test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@
4545

4646
# Raises error for missing translations.
4747
# config.action_view.raise_on_missing_translations = true
48+
49+
routes.default_url_options[:host] = 'localhost:3000'
4850
end

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
end
1313
end
1414

15-
get '/*a', to: 'application#not_found'
15+
# get '/*a', to: 'application#not_found'
1616
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
1717
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This migration comes from active_storage (originally 20170806125915)
2+
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
3+
def change
4+
create_table :active_storage_blobs do |t|
5+
t.string :key, null: false
6+
t.string :filename, null: false
7+
t.string :content_type
8+
t.text :metadata
9+
t.bigint :byte_size, null: false
10+
t.string :checksum, null: false
11+
t.datetime :created_at, null: false
12+
13+
t.index [ :key ], unique: true
14+
end
15+
16+
create_table :active_storage_attachments do |t|
17+
t.string :name, null: false
18+
t.references :record, null: false, polymorphic: true, index: false
19+
t.references :blob, null: false
20+
21+
t.datetime :created_at, null: false
22+
23+
t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
24+
t.foreign_key :active_storage_blobs, column: :blob_id
25+
end
26+
end
27+
end

db/schema.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,32 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_01_21_235038) do
13+
ActiveRecord::Schema.define(version: 2020_02_06_190329) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
1717

18+
create_table "active_storage_attachments", force: :cascade do |t|
19+
t.string "name", null: false
20+
t.string "record_type", null: false
21+
t.bigint "record_id", null: false
22+
t.bigint "blob_id", null: false
23+
t.datetime "created_at", null: false
24+
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
25+
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
26+
end
27+
28+
create_table "active_storage_blobs", force: :cascade do |t|
29+
t.string "key", null: false
30+
t.string "filename", null: false
31+
t.string "content_type"
32+
t.text "metadata"
33+
t.bigint "byte_size", null: false
34+
t.string "checksum", null: false
35+
t.datetime "created_at", null: false
36+
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
37+
end
38+
1839
create_table "categories", force: :cascade do |t|
1940
t.string "title"
2041
t.string "body"
@@ -57,6 +78,7 @@
5778
t.integer "roles_mask"
5879
end
5980

81+
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
6082
add_foreign_key "categories", "users"
6183
add_foreign_key "comments", "posts"
6284
add_foreign_key "comments", "users"

spec/factories/sample.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hello world
2+
hihi

0 commit comments

Comments
 (0)