Class: BookmarksController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/bookmarks_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /bookmarks POST /bookmarks.json



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

def create
  @bookmark = current_user.bookmarks.new(bookmark_params)

  respond_to do |format|
    if @bookmark.save
      @bookmark.tag_index!
      if params[:mode] == 'tag_edit'
        format.html { redirect_to @bookmark.manifestation , notice: t('controller.successfully_created', model: t('activerecord.models.bookmark')) }
        format.json { render json: @bookmark, status: :created, location: @bookmark }
      else
        format.html { redirect_to @bookmark , notice: t('controller.successfully_created', model: t('activerecord.models.bookmark')) }
        format.json { render json: @bookmark, status: :created, location: @bookmark }
      end
    else
      @user = current_user
      format.html { render action: "new" }
      format.json { render json: @bookmark.errors, status: :unprocessable_entity }
    end
  end

  session[:params][:bookmark] = nil if session[:params]
end

#destroyObject

DELETE /bookmarks/1 DELETE /bookmarks/1.json



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/bookmarks_controller.rb', line 128

def destroy
  @bookmark.destroy

  if @user
    respond_to do |format|
      format.html { redirect_to bookmarks_url(user_id: @user.username), notice: t('controller.successfully_deleted', model: t('activerecord.models.bookmark')) }
      format.json { head :no_content }
    end
  else
    respond_to do |format|
      format.html { redirect_to bookmarks_url(user_id: @bookmark.user.username), notice: t('controller.successfully_deleted', model: t('activerecord.models.bookmark')) }
      format.json { head :no_content }
    end
  end
end

#editObject

GET /bookmarks/1/edit



71
72
# File 'app/controllers/bookmarks_controller.rb', line 71

def edit
end

#indexObject

GET /bookmarks GET /bookmarks.json



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
# File 'app/controllers/bookmarks_controller.rb', line 8

def index
  search = Bookmark.search(include: [:manifestation])
  query = params[:query].to_s.strip
  unless query.blank?
    @query = query.dup
  end
  user = @user
  unless current_user.has_role?('Librarian')
    if user and user != current_user and !user.profile.try(:share_bookmarks)
      access_denied; return
    end
    if current_user == @user
      redirect_to bookmarks_url
      return
    end
  end

  search.build do
    fulltext query
    order_by(:created_at, :desc)
    if user
      with(:user_id).equal_to user.id
    else
      with(:user_id).equal_to current_user.id
    end
  end
  page = params[:page] || "1"
  flash[:page] = page if page.to_i >= 1
  search.query.paginate(page.to_i, Bookmark.default_per_page)
  @bookmarks = search.execute!.results

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @bookmarks }
  end
end

#newObject

GET /bookmarks/new



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/bookmarks_controller.rb', line 55

def new
  @bookmark = current_user.bookmarks.new(bookmark_params)
  manifestation = @bookmark.get_manifestation
  if manifestation
    if manifestation.bookmarked?(current_user)
      flash[:notice] = t('bookmark.already_bookmarked')
      redirect_to manifestation
      return
    end
    @bookmark.title = manifestation.original_title
  else
    @bookmark.title = Bookmark.get_title_from_url(@bookmark.url) unless @bookmark.title?
  end
end

#showObject

GET /bookmarks/1 GET /bookmarks/1.json



47
48
49
50
51
52
# File 'app/controllers/bookmarks_controller.rb', line 47

def show
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @bookmark }
  end
end

#updateObject

PUT /bookmarks/1 PUT /bookmarks/1.json



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

def update
  unless @bookmark.url.try(:bookmarkable?)
    access_denied; return
  end
  @bookmark.title = @bookmark.manifestation.try(:original_title)
  @bookmark.taggings.where(tagger_id: @bookmark.user.id).map{|t| t.destroy}

  respond_to do |format|
    if @bookmark.update_attributes(bookmark_params)
      @bookmark.tag_index!
      case params[:mode]
      when 'tag_edit'
        format.html { redirect_to @bookmark.manifestation, notice: t('controller.successfully_updated', model: t('activerecord.models.bookmark')) }
        format.json { head :no_content }
      else
        format.html { redirect_to @bookmark, notice: t('controller.successfully_updated', model: t('activerecord.models.bookmark')) }
        format.json { head :no_content }
      end
    else
      format.html { render action: "edit" }
      format.json { render json: @bookmark.errors, status: :unprocessable_entity }
    end
  end
end