Class: EnumerationsController

Inherits:
ApplicationController show all
Defined in:
frontend/app/controllers/enumerations_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Instance Method Details

#createObject



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

def create
  @enumeration = JSONModel(:enumeration).find(params[:id])

  if params[:enumeration].blank? or params[:enumeration][:value].blank?
    flash.now[:error] = "#{t("enumeration.value")} is required"
    return render_aspace_partial :partial => "new"
  end

  begin
    @enumeration.values += [params[:enumeration][:value]]
    @enumeration.save

    flash[:success] = t("enumeration._frontend.messages.created")
    render :plain => "Success"
  rescue
    flash.now[:error] = t("enumeration._frontend.messages.create_error")
    render_aspace_partial :partial => "new"
  end
end

#csvObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'frontend/app/controllers/enumerations_controller.rb', line 148

def csv
  @enumerations = JSONModel(:enumeration).all

  self.response.headers['Content-Type'] = 'text/csv'
  self.response.headers['Content-Disposition'] = "attachment; filename=enumerations_#{Time.now.strftime('%Y%m%dT%H%M%S')}.csv"
  self.response.headers['Last-Modified'] = Time.now.ctime

  self.response_body = Enumerator.new do |stream|
    JSONModel::HTTP.stream("/config/enumerations/csv") do |response|
      response.read_body do |chunk|
        stream << chunk
      end
    end
  end
end

#current_recordObject



19
20
21
# File 'frontend/app/controllers/enumerations_controller.rb', line 19

def current_record
  @enumeration
end

#deleteObject



24
25
26
27
28
29
30
31
32
33
34
# File 'frontend/app/controllers/enumerations_controller.rb', line 24

def delete
  @merge = !params["merge"].blank?
  @enumeration = JSONModel(:enumeration).find(params[:id])
  @value = params[:value]

  if @merge
    render_aspace_partial :partial => "merge"
  else
    render_aspace_partial :partial => "delete"
  end
end

#destroyObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'frontend/app/controllers/enumerations_controller.rb', line 73

def destroy
  @enumeration = JSONModel(:enumeration).find(params[:id])
  @value = params["enumeration"]["value"]
  @enumeration_value = JSONModel(:enumeration_value).find( params["enumeration"]["enumeration_value_id"])


  begin
    # ANW-1165: Unsuppress value before attempting to delete
    @enumeration_value.set_suppressed(false) if @enumeration_value["suppressed"] == true

    @enumeration.values -= [@value]
    @enumeration.save

    flash[:success] = t("enumeration._frontend.messages.deleted")
    render :plain => "Success"
  rescue ConflictException
    flash.now[:error] = t("enumeration._frontend.messages.delete_conflict")
    flash.now[:info] = t("enumeration._frontend.messages.merge_tip")

    render_aspace_partial :partial => "merge"
  rescue
    flash.now[:error] = t("enumeration._frontend.messages.delete_error")
    render_aspace_partial :partial => "delete"
  end
end

#indexObject



12
13
14
15
16
# File 'frontend/app/controllers/enumerations_controller.rb', line 12

def index
  # @enumerations = JSONModel(:enumeration).all.select{|enum| enum['editable']}
  @enumerations = JSONModel(:enumeration).all
  @enumeration = JSONModel(:enumeration).find(params[:id]) if params[:id] and not params[:id].blank?
end

#mergeObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'frontend/app/controllers/enumerations_controller.rb', line 100

def merge
  @enumeration = JSONModel(:enumeration).find(params[:id])
  @value = params["enumeration"]["value"]
  @merge = params["merge_into"]

  if @merge.blank?
    flash.now[:error] = "#{t("enumeration.merge_into")} - is required"
    return render_aspace_partial :partial => "merge"
  elsif @value.blank?
    flash.now[:error] = "#{t("enumeration.value")} - is required"
    return render_aspace_partial :partial => "merge"
  end

  begin
    request = JSONModel(:enumeration_migration).from_hash(:enum_uri => @enumeration.uri,
                                                          :from => @value,
                                                          :to => @merge)
    request.save

    flash[:success] = t("enumeration._frontend.messages.merged")
    render :plain => "Success"
  rescue
    flash.now[:error] = t("enumeration._frontend.messages.merge_error")
    render_aspace_partial :partial => "merge"
  end
end

#newObject



6
7
8
9
# File 'frontend/app/controllers/enumerations_controller.rb', line 6

def new
  @enumeration = JSONModel(:enumeration).find(params[:id])
  render_aspace_partial :partial => "new"
end

#set_defaultObject



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

def set_default
  begin
    @enumeration = JSONModel(:enumeration).find(params[:id])
    @enumeration['default_value'] = params[:value]
    @enumeration.save
    flash[:success] = t("enumeration._frontend.messages.default_set")
  rescue
    flash.now[:error] = t("enumeration._frontend.messages.default_set_error")
  end

  redirect_to(:controller => :enumerations, :action => :index, :id => params[:id])
end

#update_valueObject

we only update position and suppression here



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'frontend/app/controllers/enumerations_controller.rb', line 51

def update_value
  @enumeration_value = JSONModel(:enumeration_value).find( params[:enumeration_value_id])

  begin
    if params[:suppressed]
      suppress = ( params[:suppressed] == "1" )
      @enumeration_value.set_suppressed(suppress)
    end

    if params[:position]
      JSONModel::HTTP.post_form("#{@enumeration_value.uri}/position", :position => params[:position])
    end

    flash[:success] = t("enumeration._frontend.messages.value_updated")
  rescue
    flash.now[:error] = t("enumeration._frontend.messages.value_update_error")
  end

  redirect_to(:controller => :enumerations, :action => :index, :id => params[:id])
end