Class: FavouriteObject::BaseFavouritesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/favourite_object/base_favourites_controller.rb

Direct Known Subclasses

FavouritesController

Instance Method Summary collapse

Instance Method Details

#collectionObject



13
14
15
16
17
18
19
# File 'app/controllers/favourite_object/base_favourites_controller.rb', line 13

def collection
	@favourites = FavouriteObject::Favourite.for_owner(@user)
												  .where(is_favourited: true)
	                                              .order("created_at DESC")
	@favourites = @favourites.with_type(params[:type]) if params[:type]
	collection_pagination    
end

#collection_paginationObject



21
22
23
# File 'app/controllers/favourite_object/base_favourites_controller.rb', line 21

def collection_pagination
	@favourites = @favourites.page(params[:page]).per(params[:per_page])
end

#indexObject



7
8
9
10
11
# File 'app/controllers/favourite_object/base_favourites_controller.rb', line 7

def index
	collection

	respond_to_method
end

#queryObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/favourite_object/base_favourites_controller.rb', line 32

def query
	if params[:third_party_flag] == 'true' || params[:third_party_flag] == '1'	
		@favourite = FavouriteObject::Favourite.where(owner: @user, third_party_id: params[:target_id], 
			third_party_type: params[:target_type], third_party_flag: true).first_or_initialize
	else
		@favourite = FavouriteObject::Favourite.where(owner: @user, target_id: params[:target_id], 
			target_type: params[:target_type]).first_or_initialize
	end	

	if @favourite.is_favourited == false
		@favourite = {} 
		@favourite[:favourite] = nil 
	end

	render :json => @favourite, root: 'favourite'
end

#respond_to_methodObject



25
26
27
28
29
30
# File 'app/controllers/favourite_object/base_favourites_controller.rb', line 25

def respond_to_method
	respond_to do |format|
	  format.html
	  format.json {render :json => @favourites, meta: { pagination: { per_page: @favourites.limit_value, total_pages: @favourites.total_pages, total_objects: @favourites.total_count } }}
	end
end

#toggleObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/favourite_object/base_favourites_controller.rb', line 77

def toggle
	# toggle for web interface 
	# DOES NOT ACCOUNT FOR THIRDPARTY FAVOURITES YET
	if params[:third_party_flag] == 'true' || params[:third_party_flag] == '1'	
		favourite = FavouriteObject::Favourite.where(owner: @user, third_party_id: params[:target_id], 
			third_party_type: params[:target_type], third_party_flag: true).first_or_initialize
		favourite.params = eval(params[:params]) if params[:params]
	else
		favourite = FavouriteObject::Favourite.where(owner: @user, target_id: params[:target_id], 
			target_type: params[:target_type]).first_or_initialize
	end	
	favourite.toggle
	
	render :json => favourite, root: 'favourite'
end

#updateObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/favourite_object/base_favourites_controller.rb', line 49

def update
	#endpoint for favouriting an object
	if params[:third_party_flag] == 'true' || params[:third_party_flag] == '1'	
		favourite = FavouriteObject::Favourite.where(owner: @user, third_party_id: params[:target_id], 
			third_party_type: params[:target_type], third_party_flag: true).first_or_initialize
	else
		favourite = FavouriteObject::Favourite.where(owner: @user, target_id: params[:target_id], 
			target_type: params[:target_type]).first_or_initialize
	end	

	# favourite.params = params[:data] if params[:data]
	favourite.params = params[:data] if params[:data]
	favourite.params[:description] = params[:description] if params[:description]

	if params[:favourite] == 'true' || params[:favourite] == '1'
		favourite.is_favourited = true
		favourite.save
	else
		favourite.is_favourited = false
		favourite.destroy
		favourite = {} 
		favourite[:favourite] = nil 
	end


	render :json => favourite, root: 'favourite'
end