Class: SubscriptionsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#createObject



11
12
13
14
15
16
17
18
19
# File 'app/controllers/subscriptions_controller.rb', line 11

def create
  @subscription = Subscription.new sub_params.merge(user: current_user)
  if @subscription.save
    flash[:success] = 'Your subscription was saved successfully.'
    redirect_to params[:return_to].presence || root_path
  else
    render :error, status: :internal_server_error
  end
end

#destroyObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/subscriptions_controller.rb', line 39

def destroy
  @subscription = Subscription.find params[:id]
  if current_user.is_admin || current_user.id == @subscription.user_id
    if @subscription.destroy
      render json: { status: 'success' }
    else
      render json: { status: 'failed' }, status: :internal_server_error
    end
  else
    render json: { status: 'failed', message: 'You do not have permission to remove this subscription.' },
           status: :forbidden
  end
end

#enableObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/subscriptions_controller.rb', line 25

def enable
  @subscription = Subscription.find params[:id]
  if current_user.is_admin || current_user.id == @subscription.user_id
    if @subscription.update(enabled: params[:enabled] || false)
      render json: { status: 'success', subscription: @subscription }
    else
      render json: { status: 'failed' }, status: :internal_server_error
    end
  else
    render json: { status: 'failed', message: 'You do not have permission to update this subscription.' },
           status: :forbidden
  end
end

#indexObject



21
22
23
# File 'app/controllers/subscriptions_controller.rb', line 21

def index
  @subscriptions = current_user.subscriptions
end

#newObject



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

def new
  @phrasing = phrase_for params[:type]
  @subscription = Subscription.new
end