Skip to content

Commit

Permalink
Revised public fields, wrote more exhaustive specs
Browse files Browse the repository at this point in the history
  • Loading branch information
amiedes committed May 15, 2017
1 parent 3c7f60d commit ad8aba0
Show file tree
Hide file tree
Showing 9 changed files with 665 additions and 52 deletions.
4 changes: 4 additions & 0 deletions app/models/concerns/graphqlable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ def graphql_type_description

end

def public_created_at
self.created_at.change(min: 0)
end

end
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ def ability
end
delegate :can?, :cannot?, to: :ability

def public_proposals
public_activity? ? proposals : []
end

def public_debates
public_activity? ? debates : []
end

def public_comments
public_activity? ? comments : []
end

private

def clean_document_number
Expand Down
16 changes: 10 additions & 6 deletions app/models/vote.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
class Vote < ActsAsVotable::Vote

include Graphqlable

def self.public_for_api
joins("FULL OUTER JOIN debates ON votable_type = 'Debate' AND votable_id = debates.id").
joins("FULL OUTER JOIN proposals ON votable_type = 'Proposal' AND votable_id = proposals.id").
joins("FULL OUTER JOIN comments ON votable_type = 'Comment' AND votable_id = comments.id").
where("votable_type = 'Proposal' AND proposals.hidden_at IS NULL OR votable_type = 'Debate' AND debates.hidden_at IS NULL OR votable_type = 'Comment' AND comments.hidden_at IS NULL")
end

def public_timestamp
self.created_at.change(min: 0)
where("(votable_type = 'Proposal' AND proposals.hidden_at IS NULL) OR \
(votable_type = 'Debate' AND debates.hidden_at IS NULL) OR \
( \
(votable_type = 'Comment' AND comments.hidden_at IS NULL) AND \
( \
(comments.commentable_type = 'Proposal' AND (comments.commentable_id IN (SELECT id FROM proposals WHERE hidden_at IS NULL GROUP BY id))) OR \
(comments.commentable_type = 'Debate' AND (comments.commentable_id IN (SELECT id FROM debates WHERE hidden_at IS NULL GROUP BY id))) \
) \
)")
end
end
44 changes: 21 additions & 23 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ User:
fields:
id: integer
username: string
debates: [Debate]
proposals: [Proposal]
comments: [Comment]
organization: Organization
public_debates: [Debate]
public_proposals: [Proposal]
public_comments: [Comment]
# organization: Organization
Debate:
fields:
id: integer
title: string
description: string
created_at: string
public_created_at: string
cached_votes_total: integer
cached_votes_up: integer
cached_votes_down: integer
comments_count: integer
hot_score: integer
confidence_score: integer
geozone_id: integer
geozone: Geozone
comments: [Comment]
public_author: User
votes_for: [Vote]
Expand All @@ -34,7 +32,7 @@ Proposal:
comments_count: integer
hot_score: integer
confidence_score: integer
created_at: string
public_created_at: string
summary: string
video_url: string
geozone_id: integer
Expand All @@ -53,7 +51,7 @@ Comment:
commentable_id: integer
commentable_type: string
body: string
created_at: string
public_created_at: string
cached_votes_total: integer
cached_votes_up: integer
cached_votes_down: integer
Expand All @@ -67,11 +65,11 @@ Geozone:
name: string
ProposalNotification:
fields:
title: string
body: string
proposal_id: integer
created_at: string
proposal: Proposal
title: string
body: string
proposal_id: integer
public_created_at: string
proposal: Proposal
ActsAsTaggableOn::Tag:
fields:
id: integer
Expand All @@ -80,12 +78,12 @@ ActsAsTaggableOn::Tag:
kind: string
Vote:
fields:
votable_id: integer
votable_type: string
public_timestamp: string
vote_flag: boolean
Organization:
fields:
id: integer
user_id: integer
name: string
votable_id: integer
votable_type: string
public_created_at: string
vote_flag: boolean
# Organization:
# fields:
# id: integer
# user_id: integer
# name: string
6 changes: 1 addition & 5 deletions lib/graph_ql/api_types_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ def self.create_type(model, fields, created_types)
field(field_name, -> { created_types[field_type] }) do
resolve -> (object, arguments, context) do
association_target = object.send(field_name)
if association_target.nil?
nil
else
field_type.public_for_api.find_by(id: association_target.id)
end
association_target.present? ? field_type.public_for_api.find_by(id: association_target.id) : nil
end
end
when :multiple_association
Expand Down

0 comments on commit ad8aba0

Please sign in to comment.