class DigitalAssetsController < ApplicationController include DigitalAssetsHelper respond_to :json skip_filter(*_process_action_callbacks.map(&:filter)) rescue_from 'Mongoid::Errors::DocumentNotFound' do |exception| render json: {errors: [exception.message]}, status: 404 end # GET /digital_assets # GET /digital_assets.json #/digital_assets/search/sami_code=92023 #/digital_assets/search/sami_code=NE00192&title=Fund%20Prospectus&fund_code=20293 def index search_params = params[:search] || request.query_parameters @digital_assets = search_params.present? ? search_da(search_params) : DigitalAsset.all # respond_with(@digital_assets) render json: @digital_assets end alias :search :index # GET /digital_assets/1 # GET /digital_assets/1.json def show @digital_asset = /\w{4,8}\.\d{0,3}/ =~ params[:id] ? DigitalAsset.sami_is(params[:id]).desc(:changed_at).first : DigitalAsset.find(params[:id]) respond_with(@digital_asset) end def update da_passed = params[:digital_asset] digital_asset_id = params[:id] || da_passed[:digital_asset_id] assets = DigitalAsset.any_of({digital_asset_id: digital_asset_id}, {path: da_passed[:path]}) case assets.count when 0 #creating da = assets.build da.digital_asset_id = digital_asset_id when 1 # updating single da = assets.first else #duplicate path / id combo assets.delete_all da = DigitalAsset.find_or_initialize_by(digital_asset_id: digital_asset_id) end creating = da.new? # da_passed.delete(:digital_asset_id) # remove the ID from params hash da_passed[:digital_asset_id] = digital_asset_id # the url ID wins da.update_attributes! da_passed if(creating) head :created, location: digital_asset_path(da) else head :ok end end alias :create :update def destroy da = DigitalAsset.first(conditions: {digital_asset_id: params[:id]}) da.delete unless da.nil? head (da.nil? ? :not_found : :ok) end def updated_time if(params[:ids].present?) assets = DigitalAsset.where(:digital_asset_id.in => params[:ids]) else assets = DigitalAsset.all end updated_time = Hash[assets.map{|a| [a.digital_asset_id, a.doc_changed_at]}] render json: updated_time.to_json, status: :ok end def fund_docs @digital_assets = DigitalAsset.fund_docs.where(:fund_codes.ne => []) respond_with(@digital_assets) end end