Class: Upmin::ModelsController

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

Instance Method Summary collapse

Instance Method Details

#actionObject



92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/upmin/models_controller.rb', line 92

def action
  @response = @action.perform(@arguments)

  flash[:notice] = "Action successfully performed with a response of: #{@response}"
  redirect_to(@model.path)
  # rescue Exception => e
  #   flash.now[:alert] = "Action failed with the error message: #{e.message}"
  #   render(:show)
  # end
end

#createObject

POST /:model_name



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/upmin/models_controller.rb', line 28

def create
  @model = @klass.new
  raw_model = @model.model

  args = params[@klass.underscore_name]

  args.each do |key, value|
    # TODO(jon): Figure out a way to do transforms.

    # TODO(jon): Remove duplicate code between update and create
    if args["#{key}_is_nil"] == "1"
      raw_model.send("#{key}=", nil)
    else
      if key.ends_with?("_is_nil")
        # Skip this, since we use the non _is_nil arg.
      else
        raw_model.send("#{key}=", value)
      end
    end
  end

  if raw_model.save
    flash[:notice] = "#{@klass.humanized_name(:singular)} created successfully with id=#{raw_model.id}."
    redirect_to(@model.path)
  else
    flash.now[:alert] = "#{@klass.humanized_name(:singular)} was NOT created."
    render(:new)
  end
end

#dashboardObject



15
16
# File 'app/controllers/upmin/models_controller.rb', line 15

def dashboard
end

#newObject

GET /:model_name/new



23
24
25
# File 'app/controllers/upmin/models_controller.rb', line 23

def new
  @model = @klass.new
end

#searchObject



87
88
89
90
# File 'app/controllers/upmin/models_controller.rb', line 87

def search
  # @q = @klass.ransack(params[:q])
  # @results = Upmin::Paginator.paginate(@q.result(distinct: true), @page, 30)
end

#showObject

GET /:model_name/:id



19
20
# File 'app/controllers/upmin/models_controller.rb', line 19

def show
end

#updateObject

PUT /:model_name/:id



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/upmin/models_controller.rb', line 60

def update

  raw_model = @model.model
  updates = params[@klass.underscore_name]

  updates.each do |key, value|
    # TODO(jon): Remove duplicate code between update and create
    if updates["#{key}_is_nil"] == "1"
      raw_model.send("#{key}=", nil)
    else
      if key.ends_with?("_is_nil")
        # Skip this, since we use the non _is_nil arg.
      else
        raw_model.send("#{key}=", value)
      end
    end
  end

  if raw_model.save
    flash[:notice] = "#{@klass.humanized_name(:singular)} updated successfully."
    redirect_to(@model.path)
  else
    flash.now[:alert] = "#{@klass.humanized_name(:singular)} was NOT updated."
    render(:show)
  end
end