Class: ConstraintsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin!, #authenticate_user!, #forbidden_response, #json_request?, #record_not_found, #unauthorized_response

Instance Method Details

#constraint_paramsObject (private)


59
60
61
62
63
64
65
# File 'app/controllers/constraints_controller.rb', line 59

def constraint_params
  p = params.permit(:type, :ingredient_id, :target_id, :source_id, :min_ram, :min_cpus, :preferred_region_area, :preferred_providers =>[])
  if p[:type] == 'ProviderConstraint'
    p[:preferred_providers] = p[:preferred_providers].join(',')
  end
  p
end

#createObject


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/constraints_controller.rb', line 19

def create
  @constraint = deserialize_to_constraint
  @constraint.ingredient.destroy_all_app_recommendations if @constraint.is_a?(DependencyConstraint)
  respond_to do |format|
    if @constraint.save!
      format.html
      format.json {render json: @constraint, status: :created}
    else
      format.html
      format.json {render json: @constraint.errors, status: :unprocessable_entity}
    end
  end
end

#deserialize_to_constraintObject (private)


67
68
69
70
71
72
73
74
# File 'app/controllers/constraints_controller.rb', line 67

def deserialize_to_constraint
  type = constraint_params[:type]
  constraint_clazz = type.constantize
  constraint_instance = constraint_clazz.new
  constraint_instance.assign_attributes(constraint_params)
  constraint_instance.user = current_user
  constraint_instance
end

#destroyObject


45
46
47
48
49
50
51
# File 'app/controllers/constraints_controller.rb', line 45

def destroy
  @constraint.destroy
  respond_to do |format|
    format.html {redirect_to :back, notice: 'Constraint was successfully destroyed.' }
    format.json {head :no_content, status: :deleted}
  end
end

#indexObject


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

def index
  @constraints = current_user.constraints.page(params[:page])
  respond_to do |format|
    format.html
    format.json {render json: @constraints, status: :ok}
  end
end

#set_constraintObject (private)


55
56
57
# File 'app/controllers/constraints_controller.rb', line 55

def set_constraint
  @constraint = current_user.constraints.find(params[:id])
end

#showObject


4
5
6
7
8
9
# File 'app/controllers/constraints_controller.rb', line 4

def show
  respond_to do |format|
    format.html
    format.json {render json: @constraint, status: :ok}
  end
end

#updateObject


33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/constraints_controller.rb', line 33

def update
  respond_to do |format|
    if @constraint.update(constraint_params)
      format.html
      format.json {render json: @constraint, status: :ok}
    else
      format.html
      format.json {render json: @constraint.errors, status: :unprocessable_entity}
    end
  end
end