Skip to content

Commit

Permalink
repositories/select_options JSON #337
Browse files Browse the repository at this point in the history
  • Loading branch information
mjy committed Aug 10, 2018
1 parent 381888a commit b46fb18
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 31 deletions.
59 changes: 31 additions & 28 deletions app/controllers/namespaces_controller.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
class NamespacesController < ApplicationController

def batch_load
end

def preview_simple_batch_load
if params[:file]
@result = BatchLoad::Import::Namespaces::SimpleInterpreter.new(batch_params)
digest_cookie(params[:file].tempfile, :Simple_namespaces_md5)
render 'namespaces/batch_load/simple/preview'
else
flash[:notice] = 'No file provided!'
redirect_to action: :batch_load
end
end

def create_simple_batch_load
if params[:file] && digested_cookie_exists?(params[:file].tempfile, :Simple_namespaces_md5)
@result = BatchLoad::Import::Namespaces::SimpleInterpreter.new(batch_params)
if @result.create
flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} namespaces were created."
render 'namespaces/batch_load/simple/create' and return
else
flash[:alert] = 'Batch import failed.'
end
else
flash[:alert] = 'File to batch upload must be supplied.'
end
render :batch_load
end
include DataControllerConfiguration::SharedDataControllerConfiguration
before_action :set_namespace, only: [:show, :edit, :update, :destroy]

Expand Down Expand Up @@ -125,10 +97,41 @@ def download
send_data Download.generate_csv(Namespace.all), type: 'text', filename: "namespaces_#{DateTime.now}.csv"
end

# GET /namespaces/select_options
def select_options
@namespaces = Namespace.select_optimized(sessions_current_user_id, sessions_current_project_id, params[:klass])
end

def batch_load
end

def preview_simple_batch_load
if params[:file]
@result = BatchLoad::Import::Namespaces::SimpleInterpreter.new(batch_params)
digest_cookie(params[:file].tempfile, :Simple_namespaces_md5)
render 'namespaces/batch_load/simple/preview'
else
flash[:notice] = 'No file provided!'
redirect_to action: :batch_load
end
end

def create_simple_batch_load
if params[:file] && digested_cookie_exists?(params[:file].tempfile, :Simple_namespaces_md5)
@result = BatchLoad::Import::Namespaces::SimpleInterpreter.new(batch_params)
if @result.create
flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} namespaces were created."
render 'namespaces/batch_load/simple/create' and return
else
flash[:alert] = 'Batch import failed.'
end
else
flash[:alert] = 'File to batch upload must be supplied.'
end
render :batch_load
end


private

def set_namespace
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ def download
send_data Download.generate_csv(Repository.all), type: 'text', filename: "repositories_#{DateTime.now}.csv"
end

# GET /repositories/select_options
def select_options
@repositories = Repository.select_optimized(sessions_current_user_id, sessions_current_project_id)
end

private

# Use callbacks to share common setup or constraints between actions.
Expand Down
13 changes: 13 additions & 0 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ class Repository < ApplicationRecord
has_many :collection_objects, inverse_of: :repository, dependent: :restrict_with_error
validates_presence_of :name, :url, :acronym, :status

scope :used_recently, -> { joins(:collection_objects).where(collection_objects: { created_at: 1.weeks.ago..Time.now } ) }
scope :used_in_project, -> (project_id) { joins(:collection_objects).where( collection_objects: { project_id: project_id } ) }

def self.select_optimized(user_id, project_id)
h = {
recent: Repository.used_in_project(project_id).used_recently.limit(10).distinct.to_a,
pinboard: Repository.pinned_by(user_id).pinned_in_project(project_id).to_a
}

h[:quick] = (Repository.pinned_by(user_id).pinboard_inserted.pinned_in_project(project_id).to_a + h[:recent][0..3]).uniq
h
end

end
3 changes: 3 additions & 0 deletions app/views/repositories/_attributes.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.extract! repository, :id, :name, :url, :acronym, :status, :institutional_LSID, :is_index_herbariorum, :created_by_id, :updated_by_id, :created_at, :updated_at

json.partial! '/shared/data/all/metadata', object: repository
7 changes: 7 additions & 0 deletions app/views/repositories/select_options.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@repositories.each_key do |group|
json.set!(group) do
json.array! @repositories[group] do |n|
json.partial! '/repositories/attributes', repository: n
end
end
end
2 changes: 1 addition & 1 deletion app/views/repositories/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1 @@
json.extract! @repository, :id, :name, :url, :acronym, :status, :institutional_LSID, :is_index_herbariorum, :created_by_id, :updated_by_id, :created_at, :updated_at
json.partial! "/repositories/attributes", repository: @repository
2 changes: 1 addition & 1 deletion config/interface/hub/user_tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,4 @@ comprehensive_collection_object_task:
categories:
- collection_object
status: prototype
description: 'An all-in-one task for digitizing collectiong objects.'
description: 'An all-in-one interface for digitizing collection objects.'
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@

resources :repositories do
concerns [:data_routes]
collection do
get :select_options, defaults: {format: :json}
end
end

# TODO: add exceptions
Expand Down
16 changes: 15 additions & 1 deletion spec/models/repository_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'rails_helper'


describe Repository, type: :model do
let(:repository) {FactoryBot.build(:repository)}

Expand All @@ -19,6 +18,21 @@
end
end

context 'scopes' do
let(:r1) { FactoryBot.create(:valid_repository) }
let(:r2) { FactoryBot.create(:valid_repository) }
let!(:s1) { Specimen.create!(total: 1, repository: r1) }
let!(:s2) { Specimen.create!(total: 1, repository: r2) }

specify '#used_recently' do
expect(Repository.used_recently).to contain_exactly(r1, r2)
end

specify '#used_in_project' do
expect(Repository.used_in_project(s1.project_id)).to contain_exactly(r1, r2)
end
end

context 'concerns' do
it_behaves_like 'notable'
it_behaves_like 'is_data'
Expand Down

0 comments on commit b46fb18

Please sign in to comment.