* more HFA request polish

This commit is contained in:
Seth Call 2015-08-19 07:17:37 -05:00
parent dc343f10e3
commit d045c94f54
4 changed files with 31 additions and 18 deletions

View File

@ -13,7 +13,8 @@ CREATE TABLE jam_track_hfa_requests (
response_csv_filename VARCHAR,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
approved_at TIMESTAMP
approved_at TIMESTAMP,
received_at TIMESTAMP
);
CREATE TABLE jam_track_hfa_request_ids (
@ -23,10 +24,4 @@ CREATE TABLE jam_track_hfa_request_ids (
request_id INTEGER,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE SEQUENCE jam_track_hfa_request_id_seq;
ALTER TABLE jam_track_hfa_request_ids ALTER COLUMN request_id SET DEFAULT nextval('jam_track_hfa_request_id_seq'::regclass);
ALTER SEQUENCE jam_track_hfa_request_id_seq OWNED BY jam_track_hfa_request_ids.request_id;
ALTER TABLE ONLY jam_track_hfa_request_ids ALTER COLUMN request_id SET DEFAULT nextval('jam_track_hfa_request_id_seq'::regclass);
);

View File

@ -9,9 +9,21 @@ module JamRuby
validates :name, presence: true, length: {maximum: 200}
# look through all jam_track requests, and find the highest one that is associated with an approved harry fox requests
def self.find_max()
max = JamTrackHfaRequestId.select('max(request_id) as max').joins('INNER JOIN jam_track_hfa_requests ON jam_track_hfa_requests.id = jam_track_hfa_request_id').where('received_at IS NOT NULL').first()['max']
max.to_i
end
def self.create(name)
request = nil
transaction do
max = find_max()
start = max + 1
request = JamTrackHfaRequest.new
request.name = name
request.save!
@ -22,6 +34,8 @@ module JamRuby
request_id = JamTrackHfaRequestId.new
request_id.jam_track = jam_track
request_id.jam_track_hfa_request = request
request_id.request_id = start
start += start + 1
request_id.save
request_id.reload # to get back the request_id attribute
requests << request_id

View File

@ -2,10 +2,6 @@ module JamRuby
class JamTrackHfaRequestId < ActiveRecord::Base
include JamRuby::S3ManagerMixin
before_create(:remove_attribute)
@@log = Logging.logger[JamTrackHfaRequestId]
attr_accessible :name, as: :admin
@ -16,11 +12,6 @@ module JamRuby
validates :jam_track, presence: true
validates :jam_track_hfa_request, presence:true
private
def remove_attribute
@attributes.delete('request_id')
end
end
end

View File

@ -9,14 +9,27 @@ describe JamTrackHfaRequest do
it "creates request" do
JamTrackHfaRequest.find_max().should eq(0)
jamtrack1.touch
JamTrackHfaRequest.create('request1')
request = JamTrackHfaRequest.first
request.request_csv_filename.should_not be_nil
request.approved_at.should be_nil
request.received_at.should be_nil
request_id = JamTrackHfaRequestId.first
request_id.request_id.should_not be_nil
# as long as the previous request is not approved, we don't move on with the counter
JamTrackHfaRequest.find_max().should eq(0)
request.received_at = Time.now
request.save!
# but once it's approved, we move on
JamTrackHfaRequest.find_max().should eq(1)
end
end