Class: ClassificationsController

Inherits:
ApplicationController show all
Includes:
ExportHelper, ResultInfo
Defined in:
frontend/app/controllers/classifications_controller.rb,
public/app/controllers/classifications_controller.rb

Constant Summary collapse

IDENTIFIER_SORT_ASC =
'identifier_sort asc, repo_sort asc, title_sort asc'
IDENTIFIER_SORT_DESC =
'identifier_sort desc, repo_sort desc, title_sort desc'
DEFAULT_CL_TYPES =
%w{pui_record_group}
DEFAULT_CL_FACET_TYPES =
%w{primary_type subjects published_agents repository resource}
DEFAULT_CL_SEARCH_OPTS =
{
  'sort' => IDENTIFIER_SORT_ASC,
  'resolve[]' => ['repository:id', 'resource:id@compact_resource', 'ancestors:id@compact_resource'],
  'facet.mincount' => 1
}
DEFAULT_CL_SEARCH_PARAMS =
{
  :q => ['*'],
  :limit => 'pui_record_group',
  :op => ['OR'],
  :field => ['title']
}

Constants included from Searchable

Searchable::ABSTRACT

Instance Method Summary collapse

Methods included from ResultInfo

#breadcrumb_info, #fill_request_info, #get_rep_image, #handle_dates, #handle_external_docs, #process_agents, #process_digital, #process_digital_instance, #process_extents, #process_file_versions, #process_repo_info, #process_subjects

Methods included from ExportHelper

#csv_response, #xml_response

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Methods included from JsonHelper

#merge_notes, #process_json_notes

Methods included from Searchable

#default_search_opts, #get_filter_years, #handle_results, #html_notes, #process_results, #process_search_results, #repo_context, #search_terms, #set_up_advanced_search, #set_up_and_run_search, #set_up_search, #strip_facet_fields

Methods included from HandleFaceting

#fetch_only_facets, #get_pretty_facet_value, #strip_facets

Methods included from ManipulateNode

#inheritance, #process_mixed_content, #strip_mixed_content

Instance Method Details

#accept_childrenObject



153
154
155
# File 'frontend/app/controllers/classifications_controller.rb', line 153

def accept_children
  handle_accept_children(JSONModel(:classification))
end

#createObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'frontend/app/controllers/classifications_controller.rb', line 64

def create
  flash.keep(:spawned_from_accession)

  handle_crud(:instance => :classification,
              :on_invalid => ->() {
    render action: "new"
  },
    :on_valid => ->(id) {

    flash[:success] = I18n.t("classification._frontend.messages.created", JSONModelI18nWrapper.new(:classification => @classification))

    if @classification["is_slug_auto"] == false &&
        @classification["slug"] == nil &&
        params["classification"] &&
        params["classification"]["is_slug_auto"] == "1"

      flash[:warning] = I18n.t("slug.autogen_disabled")
    end

    redirect_to({
                :controller => :classifications,
                :action => :edit,
                :id => id
              })
  })
end

#current_recordObject



24
25
26
# File 'frontend/app/controllers/classifications_controller.rb', line 24

def current_record
  @classification
end

#defaultsObject



123
124
125
126
127
128
129
130
131
132
# File 'frontend/app/controllers/classifications_controller.rb', line 123

def defaults
  defaults = DefaultValues.get 'classification'

  values = defaults ? defaults.form_values : {}

  @classification = JSONModel(:classification).new(values)._always_valid!
  @form_title = I18n.t("default_values.form_title.classification")

  render "defaults"
end

#deleteObject



114
115
116
117
118
119
120
# File 'frontend/app/controllers/classifications_controller.rb', line 114

def delete
  classification = JSONModel(:classification).find(params[:id])
  classification.delete

  flash[:success] = I18n.t("classification._frontend.messages.deleted", JSONModelI18nWrapper.new(:classification => classification))
  redirect_to(:controller => :classifications, :action => :index, :deleted_uri => classification.uri)
end

#editObject



52
53
54
55
56
57
58
59
60
61
# File 'frontend/app/controllers/classifications_controller.rb', line 52

def edit
  flash.keep if not flash.empty? # keep the notices so they display on the subsequent ajax call

  if params[:inline]
    @classification = JSONModel(:classification).find(params[:id], find_opts)
    return render_aspace_partial :partial => "classifications/edit_inline"
  end

  @classification = JSONModel(:classification).find(params[:id])
end

#fetch_and_process(uri) ⇒ Object

we use this to get and process both classifications and classification terms



125
126
127
128
129
130
# File 'public/app/controllers/classifications_controller.rb', line 125

def fetch_and_process(uri)
  @criteria = {}
  @criteria['resolve[]'] = ['repository:id', 'resource:id@compact_resource', 'agent_uris:id']
  @result = archivesspace.get_record(uri, @criteria)
  @context = @result.breadcrumb
end

#fetch_linked_records(uri) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'public/app/controllers/classifications_controller.rb', line 132

def fetch_linked_records(uri)
  qry = "classification_uris:\"#{uri}\""
  @base_search = "#{uri}?"
  search_opts = default_search_opts(DEFAULT_CL_SEARCH_OPTS)
  search_opts['fq']=[qry]

  set_up_search(['pui'], DEFAULT_CL_FACET_TYPES, search_opts, params, qry)

  @base_search= @base_search.sub("q=#{qry}", '')
  page = Integer(params.fetch(:page, "1"))

  @results = archivesspace.search(@query, page, @criteria)

  if @results['total_hits'] > 0
    process_search_results(@base_search)
  else
    @results = []
  end
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'frontend/app/controllers/classifications_controller.rb', line 10

def index
  respond_to do |format|
    format.html {
      @search_data = Search.for_type(session[:repo_id], "classification", params_for_backend_search.merge({"facet[]" => SearchResultData.CLASSIFICATION_FACETS}))
    }
    format.csv {
      search_params = params_for_backend_search.merge({"facet[]" => SearchResultData.CLASSIFICATION_FACETS})
      search_params["type[]"] = "classification"
      uri = "/repositories/#{session[:repo_id]}/search"
      csv_response( uri, Search.build_filters(search_params), "#{I18n.t('classification._plural').downcase}." )
    }
  end
end

#newObject



39
40
41
42
43
44
45
46
47
48
49
# File 'frontend/app/controllers/classifications_controller.rb', line 39

def new
  @classification = JSONModel(:classification).new(:title => I18n.t("classification.title_default", :default => ""))._always_valid!

  if user_prefs['default_values']
    defaults = DefaultValues.get 'classification'
    @classification.update(defaults.values) if defaults
  end


  return render_aspace_partial :partial => "classifications/new_inline" if params[:inline]
end

#node_from_rootObject



170
171
172
173
174
175
# File 'frontend/app/controllers/classifications_controller.rb', line 170

def node_from_root
  classification_uri = JSONModel(:classification).uri_for(params[:id])

  render :json => JSONModel::HTTP.get_json("#{classification_uri}/tree/node_from_root",
                                           'node_ids[]' => params[:node_ids])
end

#searchObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'public/app/controllers/classifications_controller.rb', line 76

def search
    # need at least q[]=WHATEVER&op[]=OR&field[]=title&from_year[]=&to_year[]=&limit=classification
  @base_search = '/classifications/search?'
  page = Integer(params.fetch(:page, "1"))
  begin
    set_up_and_run_search( DEFAULT_CL_TYPES, DEFAULT_CL_FACET_TYPES, DEFAULT_CL_SEARCH_OPTS, params)
  rescue NoResultsError
    flash[:error] = I18n.t('search_results.no_results')
    redirect_back(fallback_location: '/') and return
  rescue Exception => error
    flash[:error] = I18n.t('errors.unexpected_error')
    redirect_back(fallback_location: '/') and return
  end
  @page_title = I18n.t('classification._plural')
  @results_type = @page_title
  @search_title = I18n.t('search_results.search_for', {:type => I18n.t('classification._plural'), :term => params.fetch(:q)[0]})
  render 'search/search_results'
end

#showObject



28
29
30
31
32
33
34
35
36
37
# File 'frontend/app/controllers/classifications_controller.rb', line 28

def show
  flash.keep

  if params[:inline]
    @classification = JSONModel(:classification).find(params[:id], find_opts)
    return render_aspace_partial :partial => "classifications/show_inline"
  end

  @classification = JSONModel(:classification).find(params[:id])
end

#termObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'public/app/controllers/classifications_controller.rb', line 109

def term
  begin
    uri = "/repositories/#{params[:rid]}/classification_terms/#{params[:id]}"
    fetch_and_process(uri)
    fetch_linked_records(uri)

    # Always show the sidebar for these
    @has_children = true

    render 'classifications/show'
  rescue RecordNotFound
    record_not_found(uri, 'classification_term')
  end
end

#treeObject



158
159
160
161
162
# File 'frontend/app/controllers/classifications_controller.rb', line 158

def tree
  flash.keep # keep the flash... just in case this fires before the form is loaded

  render :json => fetch_tree
end

#tree_nodeObject



177
178
179
180
181
182
183
184
185
186
187
# File 'frontend/app/controllers/classifications_controller.rb', line 177

def tree_node
  classification_uri = JSONModel(:classification).uri_for(params[:id])
  node_uri = if !params[:node].blank?
               params[:node]
             else
               nil
             end

  render :json => JSONModel::HTTP.get_json("#{classification_uri}/tree/node",
                                           :node_uri => node_uri)
end

#tree_node_from_rootObject



175
176
177
178
179
180
181
# File 'public/app/controllers/classifications_controller.rb', line 175

def tree_node_from_root
  @root_uri = "/repositories/#{params[:rid]}/classifications/#{params[:id]}"
  url = @root_uri + '/tree/node_from_root_' + params[:node_ids].first
  render json: archivesspace.get_raw_record(url)
rescue RecordNotFound
  render json: {}, status: 404
end

#tree_rootObject



164
165
166
167
168
# File 'frontend/app/controllers/classifications_controller.rb', line 164

def tree_root
  classification_uri = JSONModel(:classification).uri_for(params[:id])

  render :json => JSONModel::HTTP.get_json("#{classification_uri}/tree/root")
end

#tree_waypointObject



189
190
191
192
193
194
195
196
197
198
199
200
# File 'frontend/app/controllers/classifications_controller.rb', line 189

def tree_waypoint
  classification_uri = JSONModel(:classification).uri_for(params[:id])
  node_uri = if !params[:node].blank?
               params[:node]
             else
               nil
             end

  render :json => JSONModel::HTTP.get_json("#{classification_uri}/tree/waypoint",
                                           :parent_node => node_uri,
                                           :offset => params[:offset])
end

#updateObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'frontend/app/controllers/classifications_controller.rb', line 92

def update
  handle_crud(:instance => :classification,
              :obj => JSONModel(:classification).find(params[:id], find_opts),
              :on_invalid => ->() {
    render_aspace_partial :partial => "edit_inline"
  },
    :on_valid => ->(id) {
    flash.now[:success] = I18n.t("classification._frontend.messages.updated", JSONModelI18nWrapper.new(:classification => @classification))

    if @classification["is_slug_auto"] == false &&
        @classification["slug"] == nil &&
        params["classification"] &&
        params["classification"]["is_slug_auto"] == "1"

      flash.now[:warning] = I18n.t("slug.autogen_disabled")
    end

    render_aspace_partial :partial => "edit_inline"
  })
end

#update_defaultsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'frontend/app/controllers/classifications_controller.rb', line 134

def update_defaults
  begin
    DefaultValues.from_hash({
                              "record_type" => "classification",
                              "lock_version" => params[:classification].delete('lock_version'),
                              "defaults" => cleanup_params_for_schema(
                                                                      params[:classification],
                                                                      JSONModel(:classification).schema)
                            }).save

    flash[:success] = I18n.t("default_values.messages.defaults_updated")
    redirect_to :controller => :classifications, :action => :defaults
  rescue Exception => e
    flash[:error] = e.message
    redirect_to :controller => :classifications, :action => :defaults
  end
end