Class: Pbw::BaseModelsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/pbw/base_models_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#current_ability

Instance Attribute Details

#model_classObject

Returns the value of attribute model_class.



3
4
5
# File 'app/controllers/pbw/base_models_controller.rb', line 3

def model_class
  @model_class
end

Instance Method Details

#createObject



37
38
39
40
41
42
43
# File 'app/controllers/pbw/base_models_controller.rb', line 37

def create
	if @model.save
		render json: @model.to_json
	else
		render json: @model.errors.full_messages.to_json, status: :unprocessable_entity
	end
end

#destroyObject



53
54
55
56
57
58
59
# File 'app/controllers/pbw/base_models_controller.rb', line 53

def destroy
	if @model.destroy
		head :no_content
	else
		render json: @model.errors.full_messages.to_json, status: :unprocessable_entity
	end
end

#indexObject



24
25
26
27
28
29
30
31
# File 'app/controllers/pbw/base_models_controller.rb', line 24

def index
 		session[:referrer] = request.url
 		if @models && !@models.empty?
		render json: @models.to_json
	else
		render json: '', status: :unprocessable_entity
	end
end

#index_modelsObject



76
77
78
79
# File 'app/controllers/pbw/base_models_controller.rb', line 76

def index_models
	authorize! :manage, real_model_class
	@models = model_class.desc(:created_at)
end

#model_for_createObject



81
82
83
84
85
86
# File 'app/controllers/pbw/base_models_controller.rb', line 81

def model_for_create
	authorize! :create, real_model_class
	@model = real_model_class.new(params)
	@model.accessible = :all
	update_model_before_create(@model)
end

#model_for_readObject



88
89
90
91
# File 'app/controllers/pbw/base_models_controller.rb', line 88

def model_for_read
	@model = real_model_class.find(model_id)
	authorize! :read, @model
end

#model_for_updateObject



93
94
95
96
97
98
# File 'app/controllers/pbw/base_models_controller.rb', line 93

def model_for_update
	@model = real_model_class.find(model_id)
	authorize! :update, @model
	@model.accessible = :all
	update_model_before_update(@model)
end

#model_idObject



72
73
74
# File 'app/controllers/pbw/base_models_controller.rb', line 72

def model_id
	params[:id]
end

#real_model_classObject



61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/pbw/base_models_controller.rb', line 61

def real_model_class
	begin
		unless params[:_type].blank?
			klass = Kernel.get_const(params[:_type])
			return klass if klass.ancestors.include?(self.model_class)
		end
	rescue
	end
	self.model_class
end

#set_model_classObject



12
13
14
# File 'app/controllers/pbw/base_models_controller.rb', line 12

def set_model_class
	# stub method
end

#showObject



33
34
35
# File 'app/controllers/pbw/base_models_controller.rb', line 33

def show
	render json: @model.to_json
end

#updateObject



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

def update
	if @model.update_attributes(params)
		render json: @model.to_json
	else
		render json: @model.errors.full_messages.to_json, status: :unprocessable_entity
	end
end

#update_model_before_create(model) ⇒ Object



16
17
18
# File 'app/controllers/pbw/base_models_controller.rb', line 16

def update_model_before_create(model)
	# stub method
end

#update_model_before_update(model) ⇒ Object



20
21
22
# File 'app/controllers/pbw/base_models_controller.rb', line 20

def update_model_before_update(model)
	# stub method
end