favourite_object 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10e8aa16ee8823e7a4ee8e63bd9e03f4ec599f2e
4
- data.tar.gz: 9ece4107df4043ef93b43a2458a4fdde13453ff8
3
+ metadata.gz: e27704e25abb1b02018b0ba69c02b0af493123b2
4
+ data.tar.gz: 0958d771a5ad4a2241361d02436023cdb378bd85
5
5
  SHA512:
6
- metadata.gz: f61b9e5f3c8dddeecc66f7cea433d882709b8ea963a65507f2b1b8320b841c8760351c1fc8d367e883622e1a439a4e606aa06f743b914b88531aa47c367310cf
7
- data.tar.gz: c608eca028fea05206c72928394917d7db4a97ae17cdb35b74771c34f35ab064eadc6d480191357b33d471421da67f41ac7cae07b1c40a1c5880ecdf5ab9e0c8
6
+ metadata.gz: c329fd5c05b62984903606d9b5b01a4f77aad64b9a52484a095640c0859388edb6e73fa34e8840d038e62481de36785c0334c15b4be64b44fe8a33b736d12354
7
+ data.tar.gz: 7ac833d5cba4aad23823c1969f443f78f14df94fa4863f769cb28a5ddfbc8ceea7cf43c34310bbe837233dc9fb4e6386d0d5e0cbca7f597f8ddd6f23ed829622
@@ -1,100 +1,92 @@
1
- class FavouriteObject::BaseFavouritesController < ApplicationController
1
+ module FavouriteObject
2
+ class BaseFavouritesController < ApplicationController
3
+ respond_to :json
4
+ responders :json
2
5
 
3
- before_filter :authenticate!
4
- #remove before commiting
5
- skip_before_filter :verify_authenticity_token
6
+ before_filter :authenticate!
6
7
 
7
- def index
8
- collection
8
+ def index
9
+ collection
9
10
 
10
- respond_to_method
11
- end
11
+ respond_to_method(params[:serializer])
12
+ end
12
13
 
13
- def collection
14
- @favourites = FavouriteObject::Favourite.for_owner(@user)
15
- .where(is_favourited: true)
16
- .order("created_at DESC")
17
- @favourites = @favourites.with_type(params[:type]) if params[:type]
18
- collection_pagination
19
- end
14
+ def collection
15
+ @favourites = Favourite.for_owner(@user)
16
+ .where(is_favourited: true).order("created_at DESC")
20
17
 
21
- def collection_pagination
22
- @favourites = @favourites.page(params[:page]).per(params[:per_page])
23
- end
18
+ @favourites = @favourites.where(target_type: params[:target_type]) if params[:target_type]
19
+ @favourites = @favourites.where(target_id: params[:target_ids]) if params[:target_ids]
24
20
 
25
- def respond_to_method
26
- respond_to do |format|
27
- format.html
28
- format.json {render :json => @favourites, meta: { pagination: { per_page: @favourites.limit_value, total_pages: @favourites.total_pages, total_objects: @favourites.total_count } }}
21
+ collection_pagination
29
22
  end
30
- end
31
23
 
32
- def query
33
- if params[:third_party_flag] == 'true' || params[:third_party_flag] == '1'
34
- @favourite = FavouriteObject::Favourite.where(owner: @user, third_party_id: params[:target_id],
35
- third_party_type: params[:target_type], third_party_flag: true).first_or_initialize
36
- else
37
- @favourite = FavouriteObject::Favourite.where(owner: @user, target_id: params[:target_id],
38
- target_type: params[:target_type]).first_or_initialize
39
- end
40
-
41
- if @favourite.is_favourited == false
42
- @favourite = {}
43
- @favourite[:favourite] = nil
24
+ def collection_pagination
25
+ @favourites = @favourites.page(params[:page]).per(params[:per_page])
44
26
  end
45
27
 
46
- render :json => @favourite, root: 'favourite'
47
- end
28
+ def respond_to_method(serializer)
29
+ if serializer == 'lite'
30
+ return respond_with @favourites, each_serializer: LiteFavouriteSerializer
31
+ end
32
+ respond_with @favourites
33
+ end
34
+
35
+ def show
36
+ @favourite = Favourite.find_with_target(@user, params[:target_id],
37
+ params[:target_type], params[:third_party_flag])
48
38
 
49
- def update
50
- #endpoint for favouriting an object
51
- if params[:third_party_flag] == 'true' || params[:third_party_flag] == '1'
52
- favourite = FavouriteObject::Favourite.where(owner: @user, third_party_id: params[:target_id],
53
- third_party_type: params[:target_type], third_party_flag: true).first_or_initialize
54
- else
55
- favourite = FavouriteObject::Favourite.where(owner: @user, target_id: params[:target_id],
56
- target_type: params[:target_type]).first_or_initialize
57
- end
58
-
59
- # favourite.params = params[:data] if params[:data]
60
- favourite.params = params[:data] if params[:data]
61
- favourite.params[:description] = params[:description] if params[:description]
62
-
63
- if params[:favourite] == 'true' || params[:favourite] == '1'
64
- favourite.is_favourited = true
65
- favourite.save
66
- else
67
- favourite.is_favourited = false
68
- favourite.destroy
69
- favourite = {}
70
- favourite[:favourite] = nil
39
+ respond_with @favourite, serializer: LiteFavouriteSerializer
71
40
  end
72
41
 
42
+ def update
43
+ @favourite = Favourite.find_with_target(@user, params[:target_id],
44
+ params[:target_type], params[:third_party_flag])
73
45
 
74
- render :json => favourite, root: 'favourite'
75
- end
46
+ @favourite.update(permitted_params[:favourite])
76
47
 
77
- def toggle
78
- # toggle for web interface
79
- # DOES NOT ACCOUNT FOR THIRDPARTY FAVOURITES YET
80
- if params[:third_party_flag] == 'true' || params[:third_party_flag] == '1'
81
- favourite = FavouriteObject::Favourite.where(owner: @user, third_party_id: params[:target_id],
82
- third_party_type: params[:target_type], third_party_flag: true).first_or_initialize
83
- favourite.params = eval(params[:params]) if params[:params]
84
- else
85
- favourite = FavouriteObject::Favourite.where(owner: @user, target_id: params[:target_id],
86
- target_type: params[:target_type]).first_or_initialize
87
- end
88
- favourite.toggle
89
-
90
- render :json => favourite, root: 'favourite'
91
- end
48
+ respond_with @favourite, serializer: LiteFavouriteSerializer
49
+ end
50
+
51
+ def toggle
52
+ # toggle for web interface
53
+ @favourite = Favourite.find_with_target(@user, params[:target_id],
54
+ params[:target_type], params[:third_party_flag])
92
55
 
93
- private
56
+ @favourite.toggle
94
57
 
95
- def authenticate!
96
- #fix before commiting
97
- method(FavouriteObject.authentication_method).call
98
- @user = method(FavouriteObject.current_user_method).call
58
+ respond_with @favourite
59
+ end
60
+
61
+ def meta
62
+ if action_name == 'index'
63
+ {
64
+ pagination: {
65
+ per_page: @favourites.limit_value,
66
+ total_pages: @favourites.total_pages,
67
+ total_objects: @favourites.total_count
68
+ }
69
+ }
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def permitted_params
76
+ params.permit(favourite: [
77
+ :is_favourited,
78
+ :target_id,
79
+ :target_type,
80
+ :third_party_flag,
81
+ :third_party_id,
82
+ :third_party_type,
83
+ :params
84
+ ])
85
+ end
86
+
87
+ def authenticate!
88
+ method(FavouriteObject.authentication_method).call
89
+ @user = method(FavouriteObject.current_user_method).call
90
+ end
99
91
  end
100
92
  end
@@ -1,73 +1,76 @@
1
1
  module FavouriteObject
2
2
  class Favourite < ActiveRecord::Base
3
- self.table_name = "favourite_object_favourites"
3
+ self.table_name = "favourite_object_favourites"
4
4
 
5
- belongs_to :target, polymorphic: true
6
- belongs_to :owner, polymorphic: true
5
+ belongs_to :target, polymorphic: true
6
+ belongs_to :owner, polymorphic: true
7
7
 
8
- # Params for creating the notification message.
9
- serialize :params, Hash
8
+ # Params for creating the notification message.
9
+ serialize :params, Hash
10
10
 
11
- mattr_accessor :views
12
- @@views = {
13
- message: {
14
- template_path: Proc.new {|n| "favourite_object/#{n.target_type.underscore}/message" }
15
- }
16
- }
11
+ mattr_accessor :views
12
+ @@views = {
13
+ message: {
14
+ template_path: Proc.new {|n| "favourite_object/#{n.target_type.underscore}/message" }
15
+ }
16
+ }
17
17
 
18
- if ActiveRecord::VERSION::MAJOR < 4
19
- attr_accessible :target, :owner
20
- end
18
+ if ActiveRecord::VERSION::MAJOR < 4
19
+ attr_accessible :target, :owner
20
+ end
21
21
 
22
- def self.for_owner(owner)
23
- where(owner_id: owner.id)
24
- .where(owner_type: owner.class.base_class)
25
- end
22
+ def self.find_with_target(owner, target_id, target_type, third_party_flag=false)
23
+ if third_party_flag
24
+ return Favourite.for_owner(owner).find_or_initialize_by(third_party_id: target_id,
25
+ third_party_type: target_type, third_party_flag: true)
26
+ end
26
27
 
27
- def message
28
- ActionView::Base.new(
29
- Rails.configuration.paths["app/views"]).render(
30
- :template => self.class.views[:message][:template_path].call(self), :formats => [:html],
31
- :locals => {object: self.target }, :layout => false)
32
- end
28
+ Favourite.for_owner(owner).find_or_initialize_by(target_id: target_id,
29
+ target_type: target_type)
30
+ end
33
31
 
34
- #toggles the is_favourited status
35
- def toggle
36
- if is_favourited
37
- self.is_favourited = false
38
- else
39
- self.is_favourited = true
40
- end
41
- self.save
42
- end
32
+ def self.for_owner(owner)
33
+ where(owner_id: owner.id)
34
+ .where(owner_type: owner.class.base_class)
35
+ end
43
36
 
44
- def favourite
45
- self.is_favourited = true
46
- self.save
47
- end
37
+ def message
38
+ ActionView::Base.new(
39
+ Rails.configuration.paths["app/views"]).render(
40
+ :template => self.class.views[:message][:template_path].call(self), :formats => [:html],
41
+ :locals => {object: self.target }, :layout => false)
42
+ end
48
43
 
49
- def un_favourite
50
- self.is_favourited = false
51
- self.save
52
- end
44
+ #toggles the is_favourited status
45
+ def toggle
46
+ if is_favourited
47
+ self.is_favourited = false
48
+ else
49
+ self.is_favourited = true
50
+ end
51
+ self.save
52
+ end
53
53
 
54
- def self.is_favourited?(owner, target_id, target_type, third_party_flag=false)
55
- if third_party_flag
56
- favourite = FavouriteObject::Favourite.where(owner: owner, third_party_id: target_id.to_s,
57
- third_party_type: target_type).first
58
- else
59
- favourite = FavouriteObject::Favourite.where(owner: owner, target_id: target_id,
60
- target_type: target_type).first
61
- end
54
+ def favourite
55
+ self.is_favourited = true
56
+ self.save
57
+ end
62
58
 
63
- return false if favourite.blank? || favourite.is_favourited == false
59
+ def un_favourite
60
+ self.is_favourited = false
61
+ self.save
62
+ end
64
63
 
65
- return true
66
- end
64
+ def self.is_favourited?(owner, target_id, target_type, third_party_flag=false)
65
+ favourite = self.find_with_target(owner, target_id, target_type, third_party_flag)
67
66
 
68
- def self.with_type(type)
69
- where('target_type = ?', type)
70
- end
67
+ return false if favourite.blank? || favourite.is_favourited == false
68
+ true
69
+ end
70
+
71
+ def self.with_type(type)
72
+ where('target_type = ?', type)
73
+ end
71
74
 
72
75
  end
73
76
  end
@@ -1,9 +1,7 @@
1
1
  class FavouriteObject::FavouriteSerializer < ActiveModel::Serializer
2
- root :favourites
3
-
4
2
  attributes :id, :target_id, :target_type, :is_favourited, :description, :third_party_flag, :data
5
3
  embed :ids, :include => true
6
- has_one :target, polymorphic: true
4
+ # has_one :target, polymorphic: true
7
5
 
8
6
  def include_target?
9
7
  (self.object.third_party_flag != true)
@@ -14,7 +12,7 @@ class FavouriteObject::FavouriteSerializer < ActiveModel::Serializer
14
12
  self.object.params[:description]
15
13
  else
16
14
  self.object.message
17
- end
15
+ end
18
16
  end
19
17
 
20
18
  def data
@@ -22,7 +20,7 @@ class FavouriteObject::FavouriteSerializer < ActiveModel::Serializer
22
20
  self.object.params
23
21
  else
24
22
  []
25
- end
23
+ end
26
24
  end
27
25
 
28
26
  def target_id
@@ -0,0 +1,22 @@
1
+ class FavouriteObject::LiteFavouriteSerializer < ActiveModel::Serializer
2
+ root :favourite
3
+
4
+ attributes :id, :target_id, :target_type, :is_favourited, :third_party_flag
5
+
6
+ def target_id
7
+ if self.object.third_party_flag == true
8
+ self.object.third_party_id
9
+ else
10
+ self.object.target_id
11
+ end
12
+ end
13
+
14
+ def target_type
15
+ if self.object.third_party_flag == true
16
+ self.object.third_party_type
17
+ else
18
+ self.object.target_type
19
+ end
20
+ end
21
+
22
+ end
data/config/routes.rb CHANGED
@@ -1,8 +1,8 @@
1
- Rails.application.routes.draw do
2
- namespace :favourite_object do
3
- resources :favourites, only: [:index]
4
- get 'favourites/:target_type/:target_id' => 'favourites#query'
5
- put 'favourites/:target_type/:target_id' => 'favourites#update'
6
- put 'favourites/:target_type/:target_id/toggle' => 'favourites#toggle'
7
- end
8
- end
1
+ FavouriteObject::Engine.routes.draw do
2
+ namespace :favourite_object do
3
+ resources :favourites, only: [:index]
4
+ get 'favourites/:target_type/:target_id' => 'favourites#show'
5
+ put 'favourites/:target_type/:target_id' => 'favourites#update'
6
+ put 'favourites/:target_type/:target_id/toggle' => 'favourites#toggle'
7
+ end
8
+ end
@@ -1,11 +1,14 @@
1
1
  module FavouriteObject
2
2
  class Engine < ::Rails::Engine
3
+ isolate_namespace FavouriteObject
4
+
3
5
  config.generators do |g|
4
6
  g.test_framework :rspec, :fixture => false
5
7
  g.fixture_replacement :factory_girl, :dir => 'spec/factories'
6
8
  g.assets false
7
9
  g.helper false
8
10
  end
9
-
11
+
12
+ config.autoload_paths += %W(#{config.root}/lib)
10
13
  end
11
14
  end
@@ -1,3 +1,3 @@
1
1
  module FavouriteObject
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require "favourite_object/engine"
2
2
  require "kaminari"
3
3
  require "active_model_serializers"
4
-
4
+ require 'responders'
5
5
 
6
6
  module FavouriteObject
7
7
 
@@ -31,23 +31,18 @@ class FavouriteObject::InstallGenerator < Rails::Generators::Base
31
31
  # This is defined in ActiveRecord::Generators::Base, but that inherits from NamedBase, so it expects a name argument
32
32
  # which we don't want here. So we redefine it here. Yuck.
33
33
  def self.next_migration_number(dirname)
34
- if ActiveRecord::Base.timestamped_migrations
35
- Time.now.utc.strftime("%Y%m%d%H%M%S%L")
36
- else
37
- "%.3d" % (current_migration_number(dirname) + 1)
38
- end
34
+ next_migration_number = current_migration_number(dirname) + 1
35
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
39
36
  end
40
37
 
41
-
42
-
43
38
  protected
44
39
 
45
- def copy_migration(filename)
46
- if self.class.migration_exists?("db/migrate", "#{filename}")
47
- say_status("skipped", "Migration #{filename}.rb already exists")
48
- else
49
- migration_template "#{filename}.rb", "db/migrate/#{filename}.rb"
50
- end
40
+ def copy_migration(filename)
41
+ if self.class.migration_exists?("db/migrate", "#{filename}")
42
+ say_status("skipped", "Migration #{filename}.rb already exists")
43
+ else
44
+ migration_template "#{filename}.rb", "db/migrate/#{filename}.rb"
51
45
  end
46
+ end
52
47
 
53
48
  end
@@ -0,0 +1,15 @@
1
+ module Responders::JsonResponder
2
+ protected
3
+
4
+ # simply render the resource even on POST instead of redirecting for ajax
5
+ def api_behavior(error)
6
+ if post?
7
+ display resource, status: :created
8
+ # render resource instead of 204 no content
9
+ elsif put?
10
+ display resource, status: :ok
11
+ else
12
+ super
13
+ end
14
+ end
15
+ end
@@ -1,68 +1,83 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
- describe FavouriteObject::FavouritesController do
5
-
6
- let(:user) { User.create({email: "user@example.com" })}
7
- let(:arbitrary_object) { ArbitraryObject.create() }
8
-
9
- before :each do
10
- FavouriteObject::FavouritesController.any_instance.stub(:current_user).and_return(user)
11
- FavouriteObject::FavouritesController.any_instance.stub(:authenticate_user!).and_return(true)
12
- FavouriteObject::Favourite.any_instance.stub(:message).and_return("Mr. Blobby")
13
-
14
- end
15
-
16
- describe "listing favourites" do
17
- let(:favourite) { FavouriteObject::Favourite.create(owner: user, target: arbitrary_object) }
18
-
19
- before :each do
20
- favourite.is_favourited = true
21
- favourite.save
22
- end
23
-
24
- it "displays favourites as json" do
25
- get :index, format: :json
26
- response.body.should have_json_path "favourites/0/id"
27
- end
28
-
29
- it "displays favourites filtered by type" do
30
- get :index, format: :json, type: "ArbitraryObject"
31
- response.body.should have_json_path "favourites/0/id"
32
- expect(json(response.body)["favourites"].length).to eq 1
33
- end
34
-
35
- it "displays favourites web template" do
36
- get :index
37
- end
38
- end
39
-
40
- describe "unfavouriting an object" do
41
- it "returns null if favourited is equal to false" do
42
- put :update, target_type: arbitrary_object.class, target_id: arbitrary_object.id
43
- expect(json(response.body)["favourite"]).to eq nil
44
- end
45
-
46
- it "if already exists exists and is_favourited is set to false the object gets deleted" do
47
- favourite_object = FavouriteObject::Favourite.create(owner: user, target: arbitrary_object)
48
- favourite_object.is_favourited = true
49
- favourite_object.save
50
-
51
- put :update, target_type: arbitrary_object.class.name, target_id: arbitrary_object.id, favourite: false
52
-
53
- expect(json(response.body)["favourite"]).to eq nil
54
-
55
- end
56
- end
57
-
58
- describe "favourite a third_party object" do
59
- it "assigns third_party values" do
60
- put :update, target_type: "RandomClass", target_id: "object_1", third_party_flag: 'true', favourite: 'true'
61
-
62
- favourite = FavouriteObject::Favourite.last
63
- favourite.third_party_flag.should eq true
64
- favourite.third_party_id.should eq "object_1"
65
- favourite.third_party_type.should eq "RandomClass"
66
- end
67
- end
3
+ module FavouriteObject
4
+ describe FavouritesController do
5
+
6
+ let(:user) { User.create({email: "user@example.com" })}
7
+ let(:arbitrary_object) { ArbitraryObject.create() }
8
+ let(:favourite) { Favourite.create(owner: user, target: arbitrary_object) }
9
+
10
+ before :each do
11
+ FavouritesController.any_instance.stub(:current_user).and_return(user)
12
+ FavouritesController.any_instance.stub(:authenticate_user!).and_return(true)
13
+ Favourite.any_instance.stub(:message).and_return("Mr. Blobby")
14
+ end
15
+
16
+ describe 'GET show' do
17
+ it "returns 200" do
18
+ get :show, target_type: arbitrary_object.class.name, target_id: arbitrary_object.id, format: :json, use_route: :favourite_object
19
+ expect(response.response_code).to eq 200
20
+ end
21
+
22
+ it "returns favourite status" do
23
+ favourite.update(is_favourited: true)
24
+
25
+ get :show, target_type: arbitrary_object.class.name, target_id: arbitrary_object.id, format: :json, use_route: :favourite_object
26
+ expect(json[:favourite][:is_favourited]).to eq true
27
+ end
28
+ end
29
+
30
+ describe "PUT update" do
31
+ it "favouriting object returns is_favourite true" do
32
+ put :update, target_type: arbitrary_object.class.name, target_id: arbitrary_object.id, favourite: {is_favourited: true}, format: :json, use_route: :favourite_object
33
+ expect(json[:favourite][:is_favourited]).to eq true
34
+ end
35
+
36
+ it "unfavouriting object returns is_favourite false" do
37
+ put :update, target_type: arbitrary_object.class.name, target_id: arbitrary_object.id, favourite: {is_favourited: false}, format: :json, use_route: :favourite_object
38
+ expect(json[:favourite][:is_favourited]).to eq false
39
+ end
40
+ end
41
+
42
+ ## Index is used to display a list view of favourites
43
+ describe "GET index" do
44
+ it "returns 200" do
45
+ get :index, format: :json, use_route: :favourite_object
46
+ expect(response.response_code).to eq 200
47
+ end
48
+
49
+ ## doesn't include message data, which adds response_time due to rendering n templates
50
+ it "returns lite payload" do
51
+ favourite.update(is_favourited: true)
52
+
53
+ get :index, format: :json, use_route: :favourite_object, serializer: 'lite'
54
+ expect(json[:favourites][0][:description]).to be_nil
55
+ end
56
+
57
+ it "filter by type only" do
58
+ favourite.update(is_favourited: true)
59
+ Favourite.create(owner: user, target_type: 'FakeObject', target_id: 5, is_favourited: true)
60
+
61
+ get :index, format: :json, use_route: :favourite_object, target_type: 'FakeObject'
62
+ expect(json[:favourites].count).to eq 1
63
+ end
64
+
65
+ it "returns favourite filtered by type and id" do
66
+ favourite.update(is_favourited: true)
67
+ Favourite.create(owner: user, target_type: 'FakeObject', target_id: 5, is_favourited: true)
68
+
69
+ get :index, format: :json, use_route: :favourite_object, target_type: 'FakeObject', target_ids: [5]
70
+ expect(json[:favourites].count).to eq 1
71
+ end
72
+
73
+ it "doesn't return favourite with another id" do
74
+ favourite.update(is_favourited: true)
75
+ Favourite.create(owner: user, target_type: 'FakeObject', target_id: 5, is_favourited: true)
76
+
77
+ get :index, format: :json, use_route: :favourite_object, target_type: 'FakeObject', target_ids: [6]
78
+ expect(json[:favourites].count).to eq 0
79
+ end
80
+ end
81
+
82
+ end
68
83
  end