Class: Upmin::ModelsController

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

Instance Method Summary collapse

Instance Method Details

#actionObject



107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/upmin/models_controller.rb', line 107

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



25
26
27
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
# File 'app/controllers/upmin/models_controller.rb', line 25

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

#destroyObject

DELETE /:model_name/:id



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/upmin/models_controller.rb', line 89

def destroy
  raw_model = @model.model
  #destroy does not work when using datamapper and have associations but destroy! works fine
  #destroy! does not exist in ActiveRecord
  if defined?(raw_model.destroy!)
    @result = raw_model.destroy! #data mapper
  else
    @result = raw_model.destroy #active record
  end
  if @result
    flash.now[:notice] = "#{@klass.humanized_name(:singular)} deleted successfully."
    redirect_to @klass.search_path
  else
    flash.now[:alert] = "#{@klass.humanized_name(:singular)} was NOT deleted."
    render(:show)
  end
end

#newObject

GET /:model_name/new



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

def new
  @model = @klass.new
end

#searchObject



83
84
85
86
# File 'app/controllers/upmin/models_controller.rb', line 83

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

#showObject

GET /:model_name/:id



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

def show
end

#updateObject

PUT /:model_name/:id



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

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