Skip to content

Commit

Permalink
Merge pull request #59 from sanger/development
Browse files Browse the repository at this point in the history
from development (fixed sql queries)
  • Loading branch information
Proskurina authored Apr 26, 2017
2 parents 88c7278 + 38dc9ee commit d98966d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
9 changes: 6 additions & 3 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ class SmWorkflowLims < Sinatra::Base
end

get '/assets' do
presenter = AssetsController.new(params).get_index
# A list of all in progress assets
erb :'assets/index', :locals=>{:presenter=>presenter}
if params[:state].nil? && params[:identifier].nil?
redirect to("/assets?state=in_progress")
else
presenter = AssetsController.new(params).get_index
erb :'assets/index', :locals=>{:presenter=>presenter}
end
end

put '/assets' do
Expand Down
5 changes: 2 additions & 3 deletions app/models/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def remove_comment
def self.in_state(state)
if state.present?
joins(:events)
.where(events: {id: Event.latest_per_asset, state: state})
.includes(events: :state)
.merge(Event.with_last_state(state))
else
all
end
Expand Down Expand Up @@ -58,7 +57,7 @@ def set_begun_at
scope :reported, -> { reportable.completed.where.not(reported_at:nil) }
scope :latest_first, -> { order('begun_at DESC') }

default_scope { includes(:workflow,:asset_type,:comment,:batch) }
default_scope { includes(:workflow,:asset_type,:comment,:batch, :pipeline_destination, events: :state) }

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

def self.latest_per_asset
Event.group(:asset_id).maximum(:id).values
def self.latest_event_per_asset
select("MAX(id)").group("asset_id")
end

def self.with_last_state(state)
where(id: latest_event_per_asset, state_id: state.id)
end

end
2 changes: 1 addition & 1 deletion app/presenters/asset/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Index < Presenter
attr_reader :search, :assets, :total

def initialize(found_assets,search=nil,state=nil)
@total = found_assets.count
@assets = found_assets.group_by {|a| a.asset_type.name}.tap {|h| h.default = [] }
@total = found_assets.length
@search = search
@state = state.name if state
end
Expand Down
8 changes: 5 additions & 3 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
expect(event.valid?).to be true
end

it 'should know ids for latest events per asset' do
it 'should find events with particular last state' do
report_required = create :state, name: 'report_required'
in_progress_event_first_asset = Event.create!(state: in_progress, asset: asset)
in_progress_event_second_asset = Event.create!(state: in_progress, asset: (create :asset))
report_required_event_first_asset = Event.create!(state: report_required, asset: asset)
in_progress_event_second_asset = Event.create!(state: report_required, asset: (create :asset))
expect(Event.latest_per_asset).to eq [report_required_event_first_asset.id, in_progress_event_second_asset.id]
expect(Event.with_last_state(in_progress).to_a).to eq [in_progress_event_second_asset]
expect(Event.with_last_state(report_required).to_a).to eq [report_required_event_first_asset]

end


Expand Down

0 comments on commit d98966d

Please sign in to comment.