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

WIP: Move states into separate tables #1494

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ group :development, :test do
gem 'capybara'
gem 'poltergeist'
gem 'byebug'
gem 'pry'
gem 'pry-rails', group: :development
gem 'database_cleaner'
gem 'brakeman', require: false
gem 'hakiri', require: false
Expand Down
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-rails (0.3.4)
pry (>= 0.9.10)
puma (3.6.0)
rack (1.6.4)
rack-cors (0.4.0)
Expand Down Expand Up @@ -467,7 +469,7 @@ DEPENDENCIES
omniauth-saml
pg
poltergeist
pry
pry-rails
puma
rack-cors
railroady
Expand Down Expand Up @@ -497,4 +499,4 @@ RUBY VERSION
ruby 2.3.3p222

BUNDLED WITH
1.13.6
1.13.7
12 changes: 5 additions & 7 deletions app/controllers/admin/auctions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def new
end

def create
auction = BuildAuction.new(params, current_user).perform
auction = CreateAuction.new(params, current_user).perform

if SaveAuction.new(auction).perform
if auction.persisted?
flash[:success] = I18n.t('controllers.admin.auctions.create.success')
redirect_to admin_auction_path(auction)
else
Expand All @@ -33,11 +33,9 @@ def edit
def update
auction = Auction.find(params[:id])

update_auction = if ArchiveAuction.archive_submit?(params)
ArchiveAuction.new(auction: auction)
else
UpdateAuction.new(auction: auction, params: params, current_user: current_user)
end
update_auction = UpdateAuction.new(auction: auction,
params: params,
current_user: current_user)

if update_auction.perform
flash[:success] = I18n.t('controllers.admin.auctions.update.success')
Expand Down
1 change: 1 addition & 0 deletions app/models/auction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Auction < ActiveRecord::Base
belongs_to :customer
has_many :bids
has_many :bidders, through: :bids
has_many :states, class_name: 'AuctionState'
has_and_belongs_to_many :skills

has_secure_token
Expand Down
18 changes: 17 additions & 1 deletion app/models/auction_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(params, user)
end

def attributes
auction_params.merge(
@_attributes ||= auction_params.merge(
delivery_due_at: delivery_due_at,
ended_at: ended_at,
started_at: started_at,
Expand All @@ -16,8 +16,24 @@ def attributes
).delete_if { |_key, value| value.nil? }
end

def published_param
if publishing?
'published'
elsif archiving?
'archived'
end
end

private

def publishing?
attributes['published'] == 'published'
end

def archiving?
params.key? :archive_auction
end

def auction_params
strong_params.require(:auction).permit(
:billable_to,
Expand Down
3 changes: 3 additions & 0 deletions app/models/auction_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AuctionState < ActiveRecord::Base
belongs_to :auction
end
36 changes: 36 additions & 0 deletions app/services/change_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class ChangeState
def initialize(auction, state_name, state_value)
@auction = auction
@state_name = state_name
@state_value = state_value
end

def perform
state.state_value = state_value
state.save
end

def state
@state ||= find_or_build_state_record
end

def self.perform(auction, name, value)
new(auction, name, value).perform
end

private

attr_reader :auction, :state_name, :state_value

def find_or_build_state_record
find_state_record || build_state_record
end

def find_state_record
auction.states.find {|state| state.name == state_name}
end

def build_state_record
auction.states.build(name: state_name, state_value: state_value)
end
end
42 changes: 42 additions & 0 deletions app/services/create_auction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class CreateAuction
def initialize(params, current_user)
@params = params
@current_user = current_user
end

def perform
build_auction
save_auction
create_auction_states

auction
end

private

attr_reader :params, :current_user, :auction

def create_auction_states
create_published_state
create_work_state
# add more states as needed
end

def create_published_state
change_state = ChangeState.new(auction, 'published', 'unpublished')
change_state.perform
end

def create_work_state
change_state = ChangeState.new(auction, 'work', 'not_started')
change_state.perform
end

def build_auction
@auction ||= BuildAuction.new(params, current_user).perform
end

def save_auction
SaveAuction.new(auction).perform
end
end
32 changes: 26 additions & 6 deletions app/services/update_auction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,33 @@ def initialize(auction:, params:, current_user:)
end

def perform
assign_attributes
update_auction_ended_job
perform_accepted_auction_tasks
perform_rejected_auction_tasks
auction.save
create_auction_states

if ArchiveAuction.archive_submit?(params)
ArchiveAuction.new(auction: auction).perform
else
assign_attributes
update_auction_ended_job
perform_accepted_auction_tasks
perform_rejected_auction_tasks
auction.save
end
end

private

attr_reader :auction, :params, :current_user

def create_auction_states
create_published_state
Copy link
Contributor Author

@adelevie adelevie Jan 13, 2017

Choose a reason for hiding this comment

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

Let's consider changing the published state name to something like visibility.

# add more state creation here, as needed
end

Copy link
Contributor

Choose a reason for hiding this comment

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

This I get!

def create_published_state
change_state = ChangeState.new(auction, 'published', parser.published_param)
change_state.perform
end

def assign_attributes
auction.assign_attributes(parsed_attributes)
end
Expand Down Expand Up @@ -71,8 +87,12 @@ def auction_rejected?
parsed_attributes[:status] == 'rejected'
end

def parser
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just noticed I need to be smarter about the memoization. I could fix this by memoizing attributes when AuctionParser is instantiated.

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

def parsed_attributes
@_parsed_attributes ||= AuctionParser.new(params, user).attributes
parser.attributes
end

def user
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20170111193433_create_auction_states.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateAuctionStates < ActiveRecord::Migration
def change
create_table :auction_states do |t|
t.integer :auction_id
t.string :state_value
t.string :name
end
end
end
9 changes: 7 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20161221232955) do

ActiveRecord::Schema.define(version: 20170111193433) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "auction_states", force: :cascade do |t|
t.integer "auction_id"
t.string "state_value"
t.string "name"
end

create_table "auctions", force: :cascade do |t|
t.string "issue_url", default: ""
t.integer "start_price", default: 3500, null: false
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
db:
image: postgres:9
web:
tty: true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Useful for prying in using docker-compose and docker attach.

stdin_open: true
build: .
env_file: .env
environment:
Expand Down
Loading