/
expressions_controller.rb
137 lines (118 loc) · 4.27 KB
/
expressions_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
126
127
128
129
130
131
132
133
134
135
136
137
class ExpressionsController < ApplicationController
before_filter :require_user, :only => [:create, :new, :edit, :edit_in_place, :update, :destroy]
# Needlessly duplicated from works_controller. Should
# be in application.rb but I had some weird problem getting
# that working right so I'll let it ride
def edit_in_place
@expression = Expression.find(params[:id])
@expression.send "#{params[:field]}=", params[:value]
@expression.save
render :text => params[:value]
end
# Used for in_place_collection_editor, waiting to be cleaned
# up and moved to some proper home where it will be used.
def set_expression_reifications
e = Expression.find(params[:id])
work_id = params[:value]
relation = params[:relation] || '' # TODO Carry over existing relation
#render :text => "Setting up reification with work #{work_id}"
# TODO Make sure this is all properly restful.
# I'm going to ignore many-to-many relations here for now
# and remove the existing work-to-expression relation,
# just rudely dropping it, before adding a new one.
# TODO Handle many-to-many relations properly. One should be
# able to add a new relation and delete an existing one
# indepedently and RESTFULly.
e.reifications = []
r = Reification.new(:expression_id => e.id, :work_id => work_id, :relation => relation)
e.reifications << r
render :text => Work.find(work_id).anchor_text
end
# GET /expressions
# GET /expressions.xml
def index
@expressions = Expression.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @expressions }
end
end
# GET /expressions/1
# GET /expressions/1.xml
def show
@expression = Expression.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @expression }
format.rdf # show.rdf.builder
end
end
# GET /expressions/new
# GET /expressions/new.xml
def new
@expression = Expression.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @expression }
end
end
# GET /expressions/1/edit
def edit
@expression = Expression.find(params[:id])
end
# POST /expressions
# POST /expressions.xml
def create
realizer_name = params[:name]
realizer_type = params[:entity_type]
@expression = Expression.new(params[:expression])
@realizer = realizer_type._as_class.find_or_create_by_name(realizer_name)
respond_to do |format|
if @expression.save
@reification = Reification.new(:work_id => params[:work_id],
:expression_id => @expression.id,
:relation => params[:relation])
@realizer.save
@reification.save
@expression.realizers << @realizer
Work.find(params[:work_id]).reifications << @reification
flash[:notice] = 'Expression was successfully created.'
format.html { redirect_to(@expression) }
format.xml { render :xml => @expression, :status => :created, :location => @expression }
else
format.html { render :action => "new" }
format.xml { render :xml => @expression.errors, :status => :unprocessable_entity }
end
end
end
# PUT /expressions/1
# PUT /expressions/1.xml
def update
@expression = Expression.find(params[:id])
respond_to do |format|
if @expression.update_attributes(params[:expression])
flash[:notice] = 'Expression was successfully updated.'
format.html { redirect_to(@expression) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @expression.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /expressions/1
# DELETE /expressions/1.xml
def destroy
@expression = Expression.find(params[:id])
# TODO: Move into the expressions model as :after_destroy callback?
# TODO: Also need to remove Expression-Manifestation connection
@expression.reifications.each do |r|
Work.find(r.work_id).reifications.delete(r)
end
@expression.destroy
respond_to do |format|
format.html { redirect_to(expressions_url) }
format.xml { head :ok }
end
end
end