module UnlockGateway::Controller

This module will be extended (ClassMethods) and included by is_unlock_gateway on controllers

Public Instance Methods

activate() click to toggle source

This action will be used when the user requests to activate/reactivate a contribution

# File lib/unlock_gateway/controller.rb, line 31
def activate
  transition_state(:active)
end
edit() click to toggle source

A second step or final checkout should use this action

# File lib/unlock_gateway/controller.rb, line 26
def edit
  authorize @contribution
end
suspend() click to toggle source

This action will be used when the user requests to suspend a contribution

# File lib/unlock_gateway/controller.rb, line 36
def suspend
  transition_state(:suspended)
end

Private Instance Methods

contribution_params() click to toggle source

Strong parameters for Contribution

# File lib/unlock_gateway/controller.rb, line 105
def contribution_params
  params.require(:contribution).permit(*policy(@contribution || Contribution.new).permitted_attributes)
end
create_contribution() click to toggle source

Creates the contribution, sets session, returns true if successful and renders new action if not

# File lib/unlock_gateway/controller.rb, line 48
def create_contribution

  @initiative = Initiative.find(contribution_params[:initiative_id])
  @gateways = @initiative.gateways.without_state(:draft).ordered
  @contribution = @initiative.contributions.new(contribution_params)
  @contribution.gateway_state = @contribution.gateway.state
  current_user.update module_name: @contribution.gateway.module_name
  authorize @contribution

  if @contribution.save
    true
  else
    render '/initiatives/contributions/new'
    false
  end

end
set_contribution() click to toggle source

Sets @contribution

# File lib/unlock_gateway/controller.rb, line 43
def set_contribution
  @contribution = Contribution.find(params[:id])
end
transition_state(state) click to toggle source

This method authorizes @contribution, checks if the contribution can be transitioned to the desired state, calls Contribution#update_state_on_gateway!, transition the contribution's state, and return the proper JSON for Unlock's AJAX calls

# File lib/unlock_gateway/controller.rb, line 67
def transition_state(state)
  authorize @contribution
  @initiative = @contribution.initiative
  @user = @contribution.user
  state = state.to_sym
  transition = @contribution.transition_by_state(state)
  initial_state = @contribution.state_name
  resource_name = @contribution.class.model_name.human
  if @contribution.send("can_#{transition}?")
    begin
      if @contribution.state_on_gateway != state
        if @contribution.update_state_on_gateway!(state)
          @contribution.send("#{transition}!")
        else
          flash[:alert] = t('flash.actions.update.alert', resource_name: resource_name)
        end
      else
        @contribution.send("#{transition}!")
      end
    rescue
      flash[:alert] = t('flash.actions.update.alert', resource_name: resource_name)
    end
  else
    flash[:alert] = t('flash.actions.update.alert', resource_name: resource_name)
  end
  if flash[:alert].present?
    render 'initiatives/contributions/show'
  else
    if initial_state == :pending
      flash[:notice] = t('flash.actions.create.notice', resource_name: resource_name)
    else
      flash[:notice] = t('flash.actions.update.notice', resource_name: resource_name)
    end
    redirect_to initiative_contribution_path(@contribution.initiative.id, @contribution)
  end
end