Skip to content

Commit

Permalink
Merge pull request #15 from larathordar/aws
Browse files Browse the repository at this point in the history
Athletes images can be stored in aws cloud service
  • Loading branch information
holgertidemand authored Apr 6, 2018
2 parents 0286d48 + 266966c commit 2da34e6
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.3.4'
gem 'active_model_serializers', '~> 0.10.0'
gem 'aws-sdk-s3', require: false
gem 'bootsnap', '>= 1.1.0', require: false
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'rack-cors', require: 'rack/cors'
gem 'rails', '~> 5.2.0.rc2'


group :development, :test do
gem 'coveralls', require: false
gem 'factory_bot_rails'
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
aws-partitions (1.77.0)
aws-sdk-core (3.19.0)
aws-partitions (~> 1.0)
aws-sigv4 (~> 1.0)
jmespath (~> 1.0)
aws-sdk-kms (1.5.0)
aws-sdk-core (~> 3)
aws-sigv4 (~> 1.0)
aws-sdk-s3 (1.9.0)
aws-sdk-core (~> 3)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.0)
aws-sigv4 (1.0.2)
bootsnap (1.2.1)
msgpack (~> 1.0)
builder (3.2.3)
Expand Down Expand Up @@ -80,6 +93,7 @@ GEM
domain_name (~> 0.5)
i18n (1.0.0)
concurrent-ruby (~> 1.0)
jmespath (1.4.0)
json (2.1.0)
jsonapi-renderer (0.2.0)
jsonapi-rspec (0.0.2)
Expand Down Expand Up @@ -210,6 +224,7 @@ PLATFORMS

DEPENDENCIES
active_model_serializers (~> 0.10.0)
aws-sdk-s3
bootsnap (>= 1.1.0)
coveralls
factory_bot_rails
Expand Down
1 change: 1 addition & 0 deletions app/models/athlete.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Athlete < ApplicationRecord
validates :name, :age, :home, presence: true
has_one_attached :image
end
8 changes: 7 additions & 1 deletion app/serializers/athlete/show_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
class Athlete::ShowSerializer < ActiveModel::Serializer
attributes :name, :age, :home, :starttime
attributes :name, :age, :home, :starttime, :image

def image
unless object.image.attachment.nil?
object.image.service_url(expires_in: 1.hour, disposition: 'inline')
end
end
end
8 changes: 7 additions & 1 deletion app/serializers/athlete_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
class AthleteSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :name, :link, :home
attributes :name, :link, :home, :image

def link
api_v1_athlete_url(object)
end

def image
unless object.image.attachment.nil?
object.image.service_url(expires_in: 1.hour, disposition: 'inline')
end
end
end
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MckhkBgeP9c2MN502PBpQkcNBszdK5YX6k7KaWdMJa4XVU96Gj6Ah3aVXzQuFusbtbbcR1o3V6IuRd6CMZrboFcx398Q+aTnzerXlJWx6AmczRwmDLER6+MnuWw51N3bVFNZmTgfb2T3zBcrZsNHc6PUIakX3i+VGXBbtrrhGis7fmoFk4q5ay5YwtBd3YszYjVCQ3NHk4iyNimssQIZIJg+xxYFtHjec13VJdt4WCI/3sH9Cffd0iDL3Pzc9kiqLyyc8KAGe0zzeb4cofci81z+b3j0D2AkCNFP55tJ8Rm0TG8/i5WEu664X1klUDjJPx1R1gR0+B1rDrT3+bIdfI/5h4s40kqC3uFKz0uoteHAyPQeY0AY8c1yU49h/2SPZxC/HYPOQ6z6DSU+G/t6is1eDyFPvr3vhTc4--PcMzhR/7lgpZ60sb--7a8HeHPW40+uc+Z1F2h++g==
ut8x0Vl45ypRpbBsurUM/eTTubSo7cpAMEhLU/43+gNL0zbr9gXk+Qf6yWxXlBPqafusQL96YupVaQzHsBfRBYJIKuz0Ea12qY2xQKMOdqvIjjRz3L3SPfs7qFqWnj+srTupYg7pklxqTqPYQSiPFIt20dMpW17o1/FMnW2TJKeQhSypQ5cSnSWUbSGiIMgkb+FHdXbdi1XtY/LvBspDBiwsDmrX8fno9HktyuSvvn6KAdr53AC0cjtt7amyCAS2RBPab6LCYWzpCNuGpPxibIPkCqleQv3vI7hHepQRBGbfIczqwH/amM7YwSJUUKOpoz/i2s0BJ/OVc474BUtsQYB3W68fX9qJGM8UL+WfXsR29Usj3JpcnFQhOy8t25HHJ+tN5v7FWrkSc4z9Kicktm18NQeK7xrSK83lCm3dS67OmteppeFg9UFQCvTX1T9QzZH9zjvFYvyVqETEItp3ij4+99wH--NojC0boXV2MgNu89--08jQ6RGrSWm+/bR8zxWgtA==
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
config.cache_store = :null_store
end

config.active_storage.service = :local
config.active_storage.service = :amazon
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_caching = false
config.active_support.deprecation = :log
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local
config.active_storage.service = :amazon

# Mount Action Cable outside main process or domain
# config.action_cable.mount_path = nil
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr


# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
Expand Down
12 changes: 6 additions & 6 deletions config/storage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ local:
root: <%= Rails.root.join("storage") %>

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
# amazon:
# service: S3
# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
# region: us-east-1
# bucket: your_own_bucket
amazon:
service: S3
access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
region: eu-central-1
bucket: bninvitational

# Remember not to checkin your GCS keyfile to a repository
# google:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [ :key ], unique: true
end

create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false

t.datetime :created_at, null: false

t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
end
end
end
23 changes: 22 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,32 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_04_04_125327) do
ActiveRecord::Schema.define(version: 2018_04_05_180045) do

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

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "athletes", force: :cascade do |t|
t.string "name"
t.string "home"
Expand Down
Binary file added spec/fixtures/dummy_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion spec/fixtures/files/athlete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"name" => "#{athlete.name}",
"age" => athlete.age,
"home" => "#{athlete.home}",
"starttime" => athlete.starttime
"starttime" => athlete.starttime,
"image" => nil
}}}
3 changes: 2 additions & 1 deletion spec/fixtures/files/athlete_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"attributes" => {
"name" => "#{athlete.name}",
"link" => "https://votingapi.herokuapp.com/api/v1/athletes/#{athlete.id}",
"home" => "#{athlete.home}"
"home" => "#{athlete.home}",
"image" => nil
}}]}
7 changes: 7 additions & 0 deletions spec/models/athlete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@
it {is_expected.to validate_presence_of :age}
it {is_expected.to validate_presence_of :home}
end

describe 'Attachment' do
it 'is valid ' do
subject.image.attach(io: File.open(fixture_path + '/dummy_image.jpg'), filename: 'attachment.jpg', content_type: 'image/jpg')
expect(subject.image).to be_attached
end
end
end
2 changes: 1 addition & 1 deletion spec/models/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
athlete = create(:athlete)
expect(create(:result, athlete: athlete).valid?).to eq true
end
end
end

describe 'Assosiation' do
it {is_expected.to belong_to :athlete}
Expand Down
9 changes: 8 additions & 1 deletion spec/requests/api/v1/display_athletes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
RSpec.describe Api::V1::AthletesController, type: :request do
describe 'GET /api/v1/athletes' do
let!(:athlete) { create(:athlete) }
let(:object) { JSON.parse(response.body)}
let(:object) { JSON.parse(response.body) }
before do
end

it 'Should return a list of all athletes' do
get '/api/v1/athletes'
Expand All @@ -15,5 +17,10 @@
expected_response = eval(file_fixture('athlete.txt').read)
expect(object).to eq expected_response
end
it 'should return athletes image' do
athlete.image.attach(io: File.open(fixture_path + '/dummy_image.jpg'), filename: 'attachment.jpg', content_type: 'image/jpg')
get "/api/v1/athletes/#{athlete.id}"
expect(object['data']).to have_attribute(:image)
end
end
end

0 comments on commit 2da34e6

Please sign in to comment.