Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Merge 24a4d7c into b385fc6
Browse files Browse the repository at this point in the history
  • Loading branch information
jessieay committed May 18, 2016
2 parents b385fc6 + 24a4d7c commit eccff6b
Show file tree
Hide file tree
Showing 20 changed files with 233 additions and 287 deletions.
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -33,7 +33,6 @@ group :test do
gem 'webmock'
gem 'json-schema'
gem 'db-query-matchers'
gem 'json-schema'
gem 'shoulda-matchers'
gem 'sinatra'
gem 'webmock'
Expand Down
25 changes: 11 additions & 14 deletions app/controllers/admin/auctions_controller.rb
Expand Up @@ -30,28 +30,24 @@ def preview
end

def new
if params[:auction]
parser = AuctionParser.new(params)
auction = Auction.new(parser.general_attributes)
else
auction = Auction.new
end

@auction = AdminAuctionPresenter.new(auction)
@auction = Auction.new
end

def create
@auction = CreateAuction.new(params, current_user).auction
auction = AdminAuctionPresenter.new(@auction)

if @auction.save
respond_to do |format|
format.html do
flash[:success] = I18n.t('controllers.admin.auctions.create.success')
redirect_to admin_auctions_path
end

format.json do
render json: auction, serializer: Admin::AuctionSerializer
render(
json: AdminAuctionPresenter.new(@auction),
serializer: Admin::AuctionSerializer
)
end
end
else
Expand All @@ -63,21 +59,22 @@ def update
auction = Auction.find(params[:id])
UpdateAuction.new(auction, params, current_user).perform
auction.reload
auction = AdminAuctionPresenter.new(auction)

respond_to do |format|
format.html { redirect_to admin_auctions_path }
format.json do
render json: auction, serializer: Admin::AuctionSerializer
render(
json: AdminAuctionPresenter.new(auction),
serializer: Admin::AuctionSerializer
)
end
end
rescue ArgumentError => e
respond_error(e, :edit)
end

def edit
auction = Auction.find(params[:id])
@auction = AdminAuctionPresenter.new(auction)
@auction = Auction.find(params[:id])
end

private
Expand Down
9 changes: 9 additions & 0 deletions app/models/auction.rb
@@ -1,4 +1,8 @@
class Auction < ActiveRecord::Base
MAX_START_PRICE = 3500

attr_accessor :due_in_days

belongs_to :user
has_many :bids
has_many :bidders, through: :bids
Expand All @@ -10,8 +14,13 @@ class Auction < ActiveRecord::Base
# Disable STI
self.inheritance_column = :__disabled

validates :delivery_deadline, presence: true
validates :description, presence: true
validates :end_datetime, presence: true
validates :start_datetime, presence: true
validates :start_price, presence: true
validates :summary, presence: true
validates :title, presence: true
validates :user, presence: true
validate :start_price_equal_to_or_less_than_max_if_not_contracting_officer

Expand Down
92 changes: 32 additions & 60 deletions app/models/auction_parser.rb
@@ -1,86 +1,58 @@
class AuctionParser < Struct.new(:params, :user)
def attributes
general_attributes.merge(
start_datetime: start_datetime,
end_datetime: end_datetime,
user: user
)
end

def general_attributes
{
type: type,
title: title,
description: description,
summary: summary,
github_repo: github_repo,
issue_url: issue_url,
awardee_paid_status: auction_params[:awardee_paid_status],
billable_to: auction_params[:billable_to],
cap_proposal_url: auction_params[:cap_proposal_url],
delivery_deadline: delivery_deadline,
delivery_url: delivery_url,
cap_proposal_url: cap_proposal_url,
awardee_paid_status: awardee_paid_status,
published: published,
notes: notes,
billable_to: billable_to,
result: result,
start_price: start_price
}
delivery_url: auction_params[:delivery_url],
description: auction_params[:description],
end_datetime: end_datetime,
github_repo: auction_params[:github_repo],
issue_url: auction_params[:issue_url],
notes: auction_params[:notes],
published: auction_params[:published],
result: auction_params[:result],
start_datetime: start_datetime,
start_price: auction_params[:start_price],
summary: auction_params[:summary],
title: auction_params[:title],
type: auction_params[:type],
user: user
}.reject { |_key, value| value.nil? }
end

private

[
:type,
:title,
:description,
:summary,
:github_repo,
:issue_url,
:awardee_paid_status,
:published,
:awardee_paid_at,
:delivery_url,
:cap_proposal_url,
:notes,
:billable_to,
:result,
:start_price
].each do |key|
define_method key do
params[:auction][key]
end
end

def delivery_deadline
if params.key?(:due_in_days)
real_days = params[:due_in_days].to_i.business_days
end_of_workday(real_days.after(end_datetime.to_date))
else
parse_time(params[:auction][:delivery_deadline])
if auction_params.key?(:due_in_days) && auction_params[:due_in_days].present?
real_days = auction_params[:due_in_days].to_i.business_days
end_of_workday(real_days.after(end_datetime))
elsif auction_params[:deliery_deadline]
parse_datetime("delivery_deadline")
end
end

def start_datetime
parse_time(params[:auction][:start_datetime])
parse_datetime("start_datetime")
end

def end_datetime
parse_time(params[:auction][:end_datetime])
parse_datetime("end_datetime")
end

def parse_time(time)
return nil if time.nil?
parsed_time = Chronic.parse(time)
fail ArgumentError, "Missing or poorly formatted time: '#{time}'" unless parsed_time

unless time =~ /\d{1,2}:\d{2}/
parsed_time = parsed_time.beginning_of_day
def parse_datetime(field)
if auction_params["#{field}(1i)"]
Time.zone.local(*(1..5).map { |i| auction_params["#{field}(#{i}i)"] })
end
parsed_time.utc
end

def end_of_workday(date)
cob = Time.parse(BusinessTime::Config.end_of_workday)
Time.mktime(date.year, date.month, date.day, cob.hour, cob.min, cob.sec)
end

def auction_params
params[:auction]
end
end
6 changes: 4 additions & 2 deletions app/services/update_auction.rb
Expand Up @@ -15,9 +15,11 @@ def should_create_cap_proposal?

private

def attributes
parser.attributes
end

def parser
@_parser ||= AuctionParser.new(params, user)
end

delegate :attributes, to: :parser
end
11 changes: 0 additions & 11 deletions app/views/admin/auctions/_edit_attributes.html.erb

This file was deleted.

4 changes: 4 additions & 0 deletions app/views/admin/auctions/_edit_form.html.erb
@@ -0,0 +1,4 @@
<%= f.input :delivery_url %>
<%= f.input :result, collection: Auction.results.keys.to_a %>
<%= f.input :cap_proposal_url %>
<%= f.input :awardee_paid_status, collection: Auction.awardee_paid_statuses.keys.to_a %>
19 changes: 19 additions & 0 deletions app/views/admin/auctions/_form.html.erb
@@ -0,0 +1,19 @@
<%= f.input :type, collection: Auction.types.keys.to_a %>
<%= f.input :title %>
<%= f.input :github_repo %>
<%= f.input :issue_url %>
<%= f.input :summary, as: :text %>
<%= f.input :description, as: :text %>
<%= f.input :start_datetime, as: :datetime, input_html: { type: 'date' } %>
<%= f.input :end_datetime, as: :datetime %>
<% if @auction.new_record? %>
<%= f.input :due_in_days %>
<% else %>
<%= f.input :delivery_deadline, as: :datetime %>
<% end %>
<%= f.input :billable_to %>
<%= f.input :start_price %>
<%= f.input :published, collection: Auction.publisheds.keys.to_a %>
<%= f.input :notes, as: :text %>
43 changes: 0 additions & 43 deletions app/views/admin/auctions/_general_attributes.html.erb

This file was deleted.

8 changes: 4 additions & 4 deletions app/views/admin/auctions/edit.html.erb
@@ -1,6 +1,6 @@
<h1>Editing auction</h1>
<%= form_for [:admin, @auction], method: 'put' do |f| %>
<%= render partial: 'general_attributes', locals: {f: f} %>
<%= render partial: 'edit_attributes', locals: {f: f} %>
<%= f.submit value: 'Update', :class => 'usa-button usa-button-outline' %>
<%= simple_form_for [:admin, @auction] do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<%= render partial: 'edit_form', locals: { f: f } %>
<%= f.submit class: 'usa-button usa-button-outline' %>
<% end %>
8 changes: 3 additions & 5 deletions app/views/admin/auctions/new.html.erb
@@ -1,7 +1,5 @@
<h1>Create auction</h1>
<%= form_for [:admin, @auction] do |f| %>
<%= render partial: 'general_attributes', locals: {f: f} %>
<%= f.submit value: 'Create', :class => 'usa-button usa-button-outline' %>
<%= simple_form_for [:admin, @auction] do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<%= f.submit class: 'usa-button usa-button-outline' %>
<% end %>
8 changes: 8 additions & 0 deletions config/locales/en.yml
Expand Up @@ -5,6 +5,12 @@ en:
duns_number: "DUNS number"
auction:
start_price: ""
# description: ""
# title: ""
# summary: ""
# start_datetime: ""
# end_datetime: ""
# delivery_deadline: ""

errors:
models:
Expand All @@ -18,6 +24,8 @@ en:
invalid:
"You do not have permission to publish auctions with a start
price over $%{start_price}"
delivery_deadline:
blank: "(or 'Due in days') can't be blank"
controllers:
admin:
auctions:
Expand Down
15 changes: 15 additions & 0 deletions config/locales/simple_form.en.yml
Expand Up @@ -7,3 +7,18 @@ en:
mark: '*'
error_notification:
default_message: "Please review the problems below:"
labels:
auction:
start_datetime: "Start date and time (in UTC)"
end_datetime: "End date and time (in UTC)"
delivery_deadline: "Deliery deadline date and time (in UTC)"
issue_url: "GitHub issue URL"
github_repo: "GitHub repo URL"
cap_proposal_url: "CAP proposal URL"
awardee_paid_status: "Paid?"
start_price: "Start price"
helpers:
submit:
auction:
create: "Create"
update: "Update"

0 comments on commit eccff6b

Please sign in to comment.