Class: DatasetRecordsController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/dataset_records_controller.rb

Instance Method Summary collapse

Methods included from DataControllerConfiguration::ProjectDataControllerConfiguration

#annotator_params

Methods included from RedirectHelper

#destroy_redirect

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#invalid_object, #project_link, #project_matches, #project_tag, #projects_list, #projects_search_form

Methods included from Api::Intercept

#intercept_api

Methods included from TokenAuthentication

#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate

Instance Method Details

#autocomplete_data_fieldsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/dataset_records_controller.rb', line 59

def autocomplete_data_fields
  render json: {} and return if params[:field].blank? || params[:value].blank?

  import_dataset = ImportDataset.where(project_id: sessions_current_project_id).find(params[:import_dataset_id])

  values = import_dataset.core_records_fields.where(dataset_record: filtered_records)
    .at(params[:field].to_i).with_prefix_value(params[:value])
    .select(:value).distinct
    .page(params[:page]).per(params[:per] || 10)
    .map { |f| f.value }

  render json: values, status: :ok
end

#createObject

POST /dataset_records POST /dataset_records.json



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/dataset_records_controller.rb', line 20

def create
  @dataset_record = DatasetRecord.new(dataset_record_params)

  respond_to do |format|
    if @dataset_record.save
      format.html { redirect_to @dataset_record, notice: 'Dataset record was successfully created.' }
      format.json { render :show, status: :created, location: @dataset_record }
    else
      format.html { render :new }
      format.json { render json: @dataset_record.errors, status: :unprocessable_entity }
    end
  end
end

#dataset_record_paramsObject (private)

Only allow a list of trusted parameters through.



93
94
95
# File 'app/controllers/dataset_records_controller.rb', line 93

def dataset_record_params
  params.require(:dataset_record).permit(data_fields: {})
end

#destroyObject

DELETE /dataset_records/1 DELETE /dataset_records/1.json



75
76
77
78
79
80
81
# File 'app/controllers/dataset_records_controller.rb', line 75

def destroy
  @dataset_record.destroy
  respond_to do |format|
    format.html { redirect_to dataset_records_url, notice: 'Dataset record was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#filtered_recordsObject (private)



97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/dataset_records_controller.rb', line 97

def filtered_records
  import_dataset = ImportDataset.where(project_id: sessions_current_project_id).find(params[:import_dataset_id])

  dataset_records = import_dataset.core_records
  params[:filter]&.each do |key, value|
    dataset_records = dataset_records.where(
      id: import_dataset.core_records_fields.at(key.to_i).with_value(value).select(:dataset_record_id)
    )
  end

  params[:status].blank? ? dataset_records : dataset_records.where(status: params[:status])
end

#indexObject

GET /dataset_records GET /dataset_records.json



9
10
11
# File 'app/controllers/dataset_records_controller.rb', line 9

def index
  @dataset_records = filtered_records.order(id: :asc).page(params[:page]).per(params[:per] || 100) #.preload_fields
end

#set_dataset_recordObject (private)

Use callbacks to share common setup or constraints between actions.



85
86
87
88
89
90
# File 'app/controllers/dataset_records_controller.rb', line 85

def set_dataset_record
  @dataset_record = DatasetRecord.where(
    project_id: sessions_current_project_id,
    import_dataset_id: params[:import_dataset_id]
  ).find(params[:id])
end

#set_field_valueObject

PATCH/PUT /dataset_records/set_field_value



50
51
52
53
54
55
56
57
# File 'app/controllers/dataset_records_controller.rb', line 50

def set_field_value
  filtered_records.find_each(batch_size: 10000) do |record| #.preload_fields
    record.set_data_field(Integer(params[:field]), params[:value])
    record.save!
  end unless params[:filter].blank?

  render json: true, status: :ok
end

#showObject

GET /dataset_records/1 GET /dataset_records/1.json



15
16
# File 'app/controllers/dataset_records_controller.rb', line 15

def show
end

#updateObject

PATCH/PUT /dataset_records/1 PATCH/PUT /dataset_records/1.json



36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/dataset_records_controller.rb', line 36

def update
  respond_to do |format|

    JSON.parse(params[:data_fields]).each { |index, value| @dataset_record.set_data_field(index.to_i, value) }

    if @dataset_record.save
      format.json { render :show, status: :ok }
    else
      format.json { render json: @dataset_record.errors, status: :unprocessable_entity }
    end
  end
end