Class: SubjectsController

Inherits:
ApplicationController show all
Defined in:
frontend/app/controllers/subjects_controller.rb,
public/app/controllers/subjects_controller.rb

Constant Summary collapse

DEFAULT_SUBJ_TYPES =
%w{repository resource accession archival_object digital_object agent}
DEFAULT_SUBJ_FACET_TYPES =
%w{primary_type published_agents used_within_published_repository}
DEFAULT_SUBJ_SEARCH_OPTS =
{
 'sort' => 'title_sort asc',
 'resolve[]' => ['repository:id', 'resource:id@compact_resource', 'ancestors:id@compact_resource'],
 'facet.mincount' => 1
}
DEFAULT_SUBJ_SEARCH_PARAMS =
{
  :q => ['*'],
  :limit => 'subject',
  :op => ['OR'],
  :field => ['title']
}
RETAINED_PARAMETERS =
['filter_fields', 'filter_values']

Instance Method Summary collapse

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Instance Method Details

#createObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'frontend/app/controllers/subjects_controller.rb', line 53

def create
  handle_crud(:instance => :subject,
              :model => JSONModel(:subject),
              :on_invalid => ->() {
                return render_aspace_partial :partial => "subjects/new" if inline?
                return render :action => :new
              },
              :on_valid => ->(id) {
                if inline?
                  render :json => @subject.to_hash if inline?
                else
                  flash[:success] = t("subject._frontend.messages.created")

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

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

                  return redirect_to :controller => :subjects, :action => :new if params.has_key?(:plus_one)
                  redirect_to :controller => :subjects, :action => :edit, :id => id
                end
              })
end

#current_recordObject



25
26
27
# File 'frontend/app/controllers/subjects_controller.rb', line 25

def current_record
  @subject
end

#defaultsObject



100
101
102
103
104
105
106
107
108
# File 'frontend/app/controllers/subjects_controller.rb', line 100

def defaults
  defaults = DefaultValues.get 'subject'

  values = defaults ? defaults.form_values : {:vocab_id => JSONModel(:vocabulary).id_for(current_vocabulary["uri"]), :terms => [{}]}

  @subject = JSONModel(:subject).new(values)._always_valid!

  render "defaults"
end

#deleteObject



156
157
158
159
160
161
162
163
164
165
166
167
# File 'frontend/app/controllers/subjects_controller.rb', line 156

def delete
  subject = JSONModel(:subject).find(params[:id])
  begin
    subject.delete
  rescue ConflictException => e
    flash[:error] = t("subject._frontend.messages.delete_conflict", :error => t("errors.#{e.conflicts}", :default => e.message))
    return redirect_to(:controller => :subjects, :action => :show, :id => subject.id)
  end

  flash[:success] = t("subject._frontend.messages.deleted")
  redirect_to(:controller => :subjects, :action => :index, :deleted_uri => subject.uri)
end

#editObject



49
50
51
# File 'frontend/app/controllers/subjects_controller.rb', line 49

def edit
  @subject = JSONModel(:subject).find(params[:id])
end

#indexObject



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

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

#mergeObject



149
150
151
152
153
# File 'frontend/app/controllers/subjects_controller.rb', line 149

def merge
  handle_merge( params[:refs],
                JSONModel(:subject).uri_for(params[:id]),
                'subject')
end

#newObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'frontend/app/controllers/subjects_controller.rb', line 33

def new
  @subject = JSONModel(:subject).new({:vocab_id => JSONModel(:vocabulary).id_for(current_vocabulary["uri"]), :terms => [{}]})._always_valid!

  if params[:term_type]
    @subject["terms"]= [JSONModel(:term).new({:term_type => params[:term_type]})]
  end

  if user_prefs['default_values']
    defaults = DefaultValues.get 'subject'

    @subject.update(defaults.values) if defaults
  end

  render_aspace_partial :partial => "subjects/new" if inline?
end

#searchObject



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

def search
  Rails.logger.debug("we hit search!")
# need at least q[]=WHATEVER&op[]=OR&field[]=title&from_year[]=&to_year[]=&limit=subject
  @base_search = "/subjects/search?"
  page = Integer(params.fetch(:page, "1"))
  begin
    set_up_and_run_search(['subject'], DEFAULT_SUBJ_FACET_TYPES, DEFAULT_SUBJ_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: '/subjects' ) and return
  end
  @page_title = I18n.t('subject._plural')
  @results_type = @page_title
  @search_title = I18n.t('search_results.search_for', {:type => I18n.t('subject._plural'), :term => params.fetch(:q)[0]})
  @sort_opts = []
  %w(relevance title_sort_asc title_sort_desc).each do |type|
    @sort_opts.push(all_sorts[type])
  end
  @no_statement = true
  render 'search/search_results'
end

#showObject



29
30
31
# File 'frontend/app/controllers/subjects_controller.rb', line 29

def show
  @subject = JSONModel(:subject).find(params[:id])
end

#terms_completeObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'frontend/app/controllers/subjects_controller.rb', line 129

def terms_complete
  query = "#{params[:query]}".strip

  if !query.empty?
    begin
      results = JSONModel::HTTP::get_json("/terms", :q => params[:query])['results']

      return render :json => results.map {|term|
        term["_translated"] = {}
        term["_translated"]["term_type"] = t("enumerations.subject_term_type.#{term["term_type"]}")
        term
      }
    rescue
    end
  end

  render :json => []
end

#updateObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'frontend/app/controllers/subjects_controller.rb', line 80

def update
  handle_crud(:instance => :subject,
              :model => JSONModel(:subject),
              :obj => JSONModel(:subject).find(params[:id]),
              :on_invalid => ->() { return render :action => :edit },
              :on_valid => ->(id) {
                flash[:success] = t("subject._frontend.messages.updated")

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

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

                redirect_to :controller => :subjects, :action => :edit, :id => id
              })
end

#update_defaultsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'frontend/app/controllers/subjects_controller.rb', line 110

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

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