diff --git a/app/models/concerns/has_public_author.rb b/app/models/concerns/has_public_author.rb new file mode 100644 index 000000000..17cd5edd4 --- /dev/null +++ b/app/models/concerns/has_public_author.rb @@ -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 diff --git a/app/models/concerns/has_public_author_types.rb b/app/models/concerns/has_public_author_types.rb new file mode 100644 index 000000000..6555b8b4e --- /dev/null +++ b/app/models/concerns/has_public_author_types.rb @@ -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 diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 7bd4b7a92..8e67f23dd 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -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 diff --git a/config/initializers/graphql.rb b/config/initializers/graphql.rb index 306c01890..7213b64d9 100644 --- a/config/initializers/graphql.rb +++ b/config/initializers/graphql.rb @@ -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 diff --git a/lib/graph_ql/type_creator.rb b/lib/graph_ql/type_creator.rb index d0dfde580..127599f95 100644 --- a/lib/graph_ql/type_creator.rb +++ b/lib/graph_ql/type_creator.rb @@ -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