Skip to content

Commit

Permalink
Merge pull request #56 from sanger/development
Browse files Browse the repository at this point in the history
improved sql queries
  • Loading branch information
Proskurina authored Mar 30, 2017
2 parents 73e5d07 + 4554cae commit 88c7278
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 16 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ gem "pry-nav"
gem "bootstrap-sass"
gem "sinatra-assetpack"

group :development do
gem 'rack-mini-profiler'
end

group :test do
gem "rspec"
gem "timecop"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ GEM
puma (2.8.2)
rack (>= 1.1, < 2.0)
rack (1.6.4)
rack-mini-profiler (0.10.2)
rack (>= 1.2.0)
rack-protection (1.5.3)
rack
rack-test (0.6.2)
Expand Down Expand Up @@ -115,6 +117,7 @@ DEPENDENCIES
pry
pry-nav
puma
rack-mini-profiler
rake
rspec
sinatra
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def update
end

def index
assets = Asset.in_state(state).with_identifier(params[:identifier])
assets = Asset.in_state(state)
.with_identifier(params[:identifier])
Presenter::AssetPresenter::Index.new(assets, search, state)
end

Expand Down
6 changes: 4 additions & 2 deletions app/models/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def remove_comment

def self.in_state(state)
if state.present?
joins(:events).where(events: {id: Event.latest_per_asset, state: state})
joins(:events)
.where(events: {id: Event.latest_per_asset, state: state})
.includes(events: :state)
else
all
end
Expand Down Expand Up @@ -67,7 +69,7 @@ def completed?
end

def completed_at
super || events.date('completed')
super || events.detect { |event| event.state.name == 'completed' }.try(:created_at)
end

def age
Expand Down
4 changes: 0 additions & 4 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ def state_name=(state_name)
self.state = State.find_by(name: state_name)
end

def self.date(state_name)
where(state: State.find_by(name: state_name)).first.try(:created_at)
end

def self.latest_per_asset
Event.group(:asset_id).maximum(:id).values
end
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def with_each_asset_type
end

def each_workflow
Workflow.all.each do |workflow|
Workflow.all.includes(:initial_state).each do |workflow|
yield(workflow.name, workflow.has_comment, workflow.id, workflow.reportable, workflow.multi_team_quant_essential, workflow.turn_around_days)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
it 'should know if it is completed' do
asset.save
expect(asset.completed?).to be_false
create :event, asset: asset, state: completed
asset.events << (create :event, asset: asset, state: completed)
expect(asset.completed?).to be_true
end

Expand Down
6 changes: 0 additions & 6 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
expect(event.valid?).to be true
end

it 'should know the date for particular state' do
in_progress_event = Event.create!(state: in_progress, asset: asset)
expect(Event.date('reported')).to be_false
expect(Event.date('in_progress')).to be_true
end

it 'should know ids for latest events per asset' do
report_required = create :state, name: 'report_required'
in_progress_event_first_asset = Event.create!(state: in_progress, asset: asset)
Expand Down
4 changes: 3 additions & 1 deletion spec/presenters/shared_presenter_behaviour.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
flow = double("flow", name: 'flow_name')
workflow_1 = double("workflow_1", :name=>'wf1', :has_comment=>true, :id=>1, :reportable => true, multi_team_quant_essential: false, :turn_around_days=>1 )
workflow_2 = double("workflow_2", :name=>'wf2', :has_comment=>false, :id=>2, :reportable => false, multi_team_quant_essential: false, :turn_around_days=>nil)
Workflow.stub(:all) {[workflow_1,workflow_2]}
relation = double("relation")
Workflow.stub(:all) {relation}
allow(relation).to receive(:includes).and_return([workflow_1,workflow_2])

expect { |b| presenter.each_workflow(&b) }.to yield_successive_args(['wf1', true,1, true, false, 1], ['wf2', false,2, false, false, nil])
end
Expand Down

0 comments on commit 88c7278

Please sign in to comment.