Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/controllers/ad_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def view
target_ids = Ad.pluck(:id).sample(params[:count].to_i)
ads = Ad.find(target_ids)
ads.each do |ad|
report = Report.find_by(ad_id: ad.id, adspot_id: params[:adspot_id],date: Date.today)
report = Report.find_by(ad_id: ad.id, adspot_id: params[:adspot_id], date: Date.today)
unless report
report = Report.new(ad_id: ad.id, adspot_id: params[:adspot_id], date: Date.today)
end
Expand All @@ -27,7 +27,7 @@ def view
end

def click
report = Report.find_by(ad_id: params[:ad_id], adspot_id: params[:adspot_id],date: Date.today)
report = Report.find_by(ad_id: params[:ad_id], adspot_id: params[:adspot_id], date: Date.today)
unless report
report = Report.new(ad_id: params[:ad_id], adspot_id: params[:adspot_id], date: Date.today)
end
Expand All @@ -37,4 +37,3 @@ def click
end

end

24 changes: 23 additions & 1 deletion app/controllers/ad_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
require 'date'
class AdController < ApplicationController
def index
@ads = Ad.all
Expand Down Expand Up @@ -38,9 +39,30 @@ def destroy
redirect_to(ad_index_path)
end

def report #report.htmlに全レポートを出力
@reports = Report.select("
ad_id,
SUM(reports.imp) AS imp,
SUM(reports.click) AS click,
SUM(reports.cv) AS cv,
SUM(reports.price) AS price
").group(:ad_id)
end

def report_period #report.htmlに一定期間のレポートを出力
@reports = Report.select("
ad_id,
SUM(reports.imp) AS imp,
SUM(reports.click) AS click,
SUM(reports.cv) AS cv,
SUM(reports.price) AS price
").where(date: Date.parse(params[:date_min])..Date.parse(params[:date_max])).group(:ad_id)
render('/ad/report')
end

private

def ad_params # Adオブジェクト作成時にフォームから入力したパラメーターを渡す。
params.require(:ad).permit(:price, :text, :advertiser_id, :image)
params.require(:ad).permit(:price, :text, :image)
end
end
2 changes: 1 addition & 1 deletion app/models/ad.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Ad < ApplicationRecord
validates :text, presence: true
validates :price, presence: true
validates :image, presence: true
validates :advertiser_id, presence: true
has_many :reports

end
1 change: 1 addition & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
class Report < ApplicationRecord
belongs_to :ad
end
2 changes: 0 additions & 2 deletions app/views/ad/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<%= f.text_area :text,value: @ad.text,class: "form-control" %>
<p>price</p>
<%= f.number_field :price,value: @ad.price %>
<p>advertiser_id</p>
<%= f.number_field :advertiser_id,value: @ad.advertiser_id %>
<p>image</p>
<%= f.file_field :image,value: @ad.image,class: "form-control-file" %>
<%= f.submit class: "btn btn-primary",value: "更新" %>
Expand Down
2 changes: 0 additions & 2 deletions app/views/ad/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<%= f.text_area :text,value: @ad.text,class: "form-control" %>
<p>price</p>
<%= f.number_field :price,value: @ad.price %>
<p>advertiser_id</p>
<%= f.number_field :advertiser_id,value: @ad.advertiser_id %>
<p>image</p>
<%= f.file_field :image,value: @ad.image,class: "form-control-file" %>
<%= f.submit class: "btn btn-primary",value: "入稿" %>
Expand Down
33 changes: 33 additions & 0 deletions app/views/ad/report.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= form_tag(report_period_path, method: :get) do %>
<%= date_field_tag :date_min %>
<%= date_field_tag :date_max %>
<%= submit_tag "期間指定", class: "btn btn-primary" %>
<% end %>
<div class="ads">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" class="h2">広告ID</th>
<th scope="col" class="h2">Impression数</th>
<th scope="col" class="h2">Click数</th>
<th scope="col" class="h2">総費用(円)</th>
<th scope="col" class="h2">CTR</th>
<th scope="col" class="h2">CPM</th>
</tr>
</thead>
<tbody>
<tr>
<% @reports.each do |report| %>
<td class="h3 align-middle"><%= report.ad_id %></td>
<td class="h3 align-middle"><%= report.imp %></td>
<td class="h3 align-middle"><%= report.click %></td>
<td class="h3 align-middle"><%= report.price %></td>
<td class="h3 align-middle">
<%= (report.click.to_f / report.imp.to_f).round(3) %></td>
<td class="h3 align-middle">
<%= (report.price / (1000 / report.imp)) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
get '/view' => 'ad_api#view'
get '/click' => 'ad_api#click'
resources :ad
get '/report' => 'ad#report'
get '/report_period' => 'ad#report_period'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
1 change: 0 additions & 1 deletion db/migrate/20190528085221_create_ads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
class CreateAds < ActiveRecord::Migration[5.2] # Reportテーブルのtotalpriceにpriceを加算する為に使用しているのでここでも使っています
def change
create_table :ads do |t|
t.integer :advertiser_id, null: false # 広告主ID
t.string :image, null: false, default: '' # 広告の画像URL
t.integer :price, null: false # 広告の価格
t.string :text, null: false, default: '' # 広告の説明文
Expand Down
1 change: 0 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
ActiveRecord::Schema.define(version: 2019_06_14_041856) do

create_table "ads", force: :cascade do |t|
t.integer "advertiser_id", null: false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テーブル定義は消して大丈夫?
広告を作成する時に広告主IDを指定しないだけで、広告主に広告が紐づく構造は変わらないから消したらマズイ気が

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すり合わせが必要とかだったら明日やろう

t.string "image", default: "", null: false
t.integer "price", null: false
t.string "text", default: "", null: false
Expand Down
34 changes: 3 additions & 31 deletions spec/controllers/ad_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
expect do
post :create, params: { ad: { 'text' => 'Updated!',
'price' => 1010101,
'advertiser_id' => 1010101,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) } }
end.to change(Ad, :count).by(1)
expect(response).to redirect_to(ad_index_path)
Expand All @@ -16,31 +15,20 @@
it "without text should render 'new'" do
post :create, params: { ad: { 'text' => nil,
'price' => 1010101,
'advertiser_id' => 1010101,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) } }
expect(response).to render_template :new
end

it "without price should render 'new'" do
post :create, params: { ad: { 'text' => 'Updated!',
'price' => nil,
'advertiser_id' => 1010101,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) } }
expect(response).to render_template :new
end

it "without advertiser_id should render 'new'" do
post :create, params: { ad: { 'text' => 'Updated!',
'price' => 1010101,
'advertiser_id' => nil,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) } }
expect(response).to render_template :new
end

it "without image should render 'new'" do
post :create, params: { ad: { 'text' => 'Updated!',
'price' => 1010101,
'advertiser_id' => 1010101,
'image' => nil } }
expect(response).to render_template :new
end
Expand All @@ -56,10 +44,8 @@
patch :update, params: { ad: {
'text' => nil,
'price' => nil,
'advertiser_id' => nil,
'image' => nil
},
id: @ad.id }
},id: @ad.id }
expect(response).to render_template :edit
end

Expand All @@ -73,11 +59,6 @@
expect(response).to redirect_to(ad_index_path)
end

it 'with advertiser_id should be redirected' do
patch :update, params: { ad: { 'advertiser_id' => 1010101 }, id: @ad.id }
expect(response).to redirect_to(ad_index_path)
end

it 'with image should be redirected' do
patch :update, params: { ad: { 'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) }, id: @ad.id }
expect(response).to redirect_to(ad_index_path)
Expand All @@ -87,10 +68,8 @@
patch :update, params: { ad: {
'text' => 'Updated!',
'price' => 1010101,
'advertiser_id' => 1010101,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg'))
},
id: @ad.id }
},id: @ad.id }
expect(response).to redirect_to(ad_index_path)
end
end
Expand All @@ -106,11 +85,6 @@
expect(response).to render_template :edit
end

it 'without advertiser_id should be rendered' do
patch :update, params: { ad: { 'advertiser_id' => nil }, id: @ad.id }
expect(response).to render_template :edit
end

it 'without image should be rendered' do
patch :update, params: { ad: { 'image' => nil }, id: @ad.id }
expect(response).to render_template :edit
Expand All @@ -120,10 +94,8 @@
patch :update, params: { ad: {
'text' => nil,
'price' => nil,
'advertiser_id' => nil,
'image' => nil
},
id: @ad.id }
},id: @ad.id }
expect(response).to render_template :edit
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/factories/ads.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true
FactoryBot.define do
factory :ad do
advertiser_id { 1 }
image { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) }
price { 1 }
text { 'Test Text' }
Expand Down
3 changes: 0 additions & 3 deletions spec/features/path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
it 'is Forms valid?' do
fill_in 'ad_text', with: 'TextForm'
fill_in 'ad_price', with: '1000'
fill_in 'ad_advertiser_id', with: '1'
fill_in 'ad_text', with: 'TextForm'
page.attach_file('ad_image', '/Users/hashimototakuma/Downloads/PNG_transparency_demonstration_1.png')
end

it 'is create action valid?' do
fill_in 'ad_text', with: 'TextForm'
fill_in 'ad_price', with: '1000'
fill_in 'ad_advertiser_id', with: '1'
fill_in 'ad_text', with: 'TextForm'
page.attach_file('ad_image', '/Users/hashimototakuma/Downloads/PNG_transparency_demonstration_1.png')

Expand Down Expand Up @@ -53,7 +51,6 @@
visit 'ad/new'
fill_in 'ad_text', with: 'TextForm'
fill_in 'ad_price', with: '1000'
fill_in 'ad_advertiser_id', with: '1'
fill_in 'ad_text', with: 'TextForm'
page.attach_file('ad_image', '/Users/hashimototakuma/Downloads/PNG_transparency_demonstration_1.png')
click_button '入稿'
Expand Down
5 changes: 0 additions & 5 deletions spec/models/ad_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
expect(@ad).not_to be_valid
end

it 'without advertiser_id should be invalid' do
@ad.advertiser_id = nil
expect(@ad).not_to be_valid
end

it 'without image should be invalid' do
@ad.image = nil
expect(@ad).not_to be_valid
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/ads.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
advertiser_id: 1
image: MyString
price: MyString
text: MyString

two:
advertiser_id: 1
image: MyString
price: MyString
text: MyString