Expose public_author for proposals

This commit is contained in:
Alberto Miedes Garcés
2016-12-11 15:42:17 +01:00
parent b2730b6239
commit 82954c922a
5 changed files with 28 additions and 7 deletions

View File

@@ -0,0 +1,9 @@
module HasPublicAuthor
def public_author_id
public_author.try(:id)
end
def public_author
self.author.public_activity? ? self.author : nil
end
end

View File

@@ -0,0 +1,9 @@
module HasPublicAuthorTypes
def public_author_id_type
GraphQL::INT_TYPE
end
def public_author_type
TypeCreator.created_types[User]
end
end

View File

@@ -6,6 +6,8 @@ class Proposal < ActiveRecord::Base
include Sanitizable
include Searchable
include Filterable
include HasPublicAuthor
extend HasPublicAuthorTypes
acts_as_votable
acts_as_paranoid column: :hidden_at

View File

@@ -1,15 +1,15 @@
API_TYPE_DEFINITIONS = {
User => %I[ id username proposals organization ],
Debate => %I[ id title description author_id author created_at comments ],
Proposal => %I[ id title description author_id author created_at comments ],
Proposal => %I[ id title description author_id author public_author_id public_author created_at comments ],
Comment => %I[ id body user_id user commentable_id ],
Organization => %I[ id name ]
}
type_creator = GraphQL::TypeCreator.new
TypeCreator = GraphQL::TypeCreator.new
API_TYPE_DEFINITIONS.each do |model, fields|
type_creator.create(model, fields)
TypeCreator.create(model, fields)
end
ConsulSchema = GraphQL::Schema.define do
@@ -29,7 +29,7 @@ QueryRoot = GraphQL::ObjectType.define do
name "Query"
description "The query root for this schema"
type_creator.created_types.each do |model, created_type|
TypeCreator.created_types.each do |model, created_type|
# create an entry field to retrive a single object
field model.name.underscore.to_sym do

View File

@@ -22,17 +22,18 @@ module GraphQL
name(model.name)
description("#{model.model_name.human}")
# Make a field for each column
# Make a field for each column, association or method
field_names.each do |field_name|
if model.column_names.include?(field_name.to_s)
field(field_name.to_s, TYPES_CONVERSION[model.columns_hash[field_name.to_s].type])
else
association = type_creator.class.association?(model, field_name)
elsif association = type_creator.class.association?(model, field_name)
if type_creator.class.needs_pagination?(association)
connection association.name, -> { type_creator.created_types[association.klass].connection_type }
else
field association.name, -> { type_creator.created_types[association.klass] }
end
else
field field_name.to_s, model.send("#{field_name}_type".to_sym)
end
end
end