From d045c94f54095bd413add20fde3555d8c32279a1 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Wed, 19 Aug 2015 07:17:37 -0500 Subject: [PATCH] * more HFA request polish --- db/up/harry_fox_agency.sql | 11 +++-------- ruby/lib/jam_ruby/models/jam_track_hfa_request.rb | 14 ++++++++++++++ .../jam_ruby/models/jam_track_hfa_request_id.rb | 9 --------- .../jam_ruby/models/jam_track_hfa_request_spec.rb | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/db/up/harry_fox_agency.sql b/db/up/harry_fox_agency.sql index dfc94be59..7c8371a17 100644 --- a/db/up/harry_fox_agency.sql +++ b/db/up/harry_fox_agency.sql @@ -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); \ No newline at end of file +); \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/jam_track_hfa_request.rb b/ruby/lib/jam_ruby/models/jam_track_hfa_request.rb index 3733a6574..31d9f59bf 100644 --- a/ruby/lib/jam_ruby/models/jam_track_hfa_request.rb +++ b/ruby/lib/jam_ruby/models/jam_track_hfa_request.rb @@ -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 diff --git a/ruby/lib/jam_ruby/models/jam_track_hfa_request_id.rb b/ruby/lib/jam_ruby/models/jam_track_hfa_request_id.rb index 81d365e33..8dab5fb5c 100644 --- a/ruby/lib/jam_ruby/models/jam_track_hfa_request_id.rb +++ b/ruby/lib/jam_ruby/models/jam_track_hfa_request_id.rb @@ -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 diff --git a/ruby/spec/jam_ruby/models/jam_track_hfa_request_spec.rb b/ruby/spec/jam_ruby/models/jam_track_hfa_request_spec.rb index 9318b620a..146a7f565 100644 --- a/ruby/spec/jam_ruby/models/jam_track_hfa_request_spec.rb +++ b/ruby/spec/jam_ruby/models/jam_track_hfa_request_spec.rb @@ -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