Class: Collavre::InboxItemsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/collavre/inbox_items_controller.rb

Constant Summary collapse

PER_PAGE =
20

Instance Method Summary collapse

Instance Method Details

#countObject



35
36
37
38
# File 'app/controllers/collavre/inbox_items_controller.rb', line 35

def count
  c = InboxItem.where(owner: Current.user, state: "new").count
  render json: { count: c }
end

#destroyObject



49
50
51
52
53
54
55
56
# File 'app/controllers/collavre/inbox_items_controller.rb', line 49

def destroy
  if @inbox_item.owner == Current.user
    @inbox_item.destroy
    head :no_content
  else
    head :forbidden
  end
end

#indexObject



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

def index
  scope = InboxItem.where(owner: Current.user).order(created_at: :desc)
  scope = scope.new_items unless params[:show] == "all"

  per_page = params[:per_page].presence&.to_i || PER_PAGE
  per_page = PER_PAGE if per_page <= 0 || per_page > 100
  page = params[:page].presence&.to_i || 1
  page = 1 if page < 1
  offset = (page - 1) * per_page

  items = scope.offset(offset).limit(per_page + 1).to_a
  @inbox_items = items.first(per_page)
  @next_page = items.length > per_page ? page + 1 : nil

  respond_to do |format|
    format.html do
      render partial: "collavre/inbox_items/list", locals: { items: @inbox_items, next_page: @next_page }
    end
    format.json do
      render json: {
        items_html: render_to_string(partial: "collavre/inbox_items/items", formats: [ :html ], locals: { items: @inbox_items }),
        next_page: @next_page,
        empty: @inbox_items.empty?
      }
    end
  end
end

#updateObject



40
41
42
43
44
45
46
47
# File 'app/controllers/collavre/inbox_items_controller.rb', line 40

def update
  if @inbox_item.owner == Current.user
    @inbox_item.update(state: params[:state])
    head :no_content
  else
    head :forbidden
  end
end