Expose Proposal public_author using scopes instead of modules

This commit is contained in:
Alberto Miedes Garcés
2016-12-13 14:04:53 +01:00
parent 82954c922a
commit 402b07d959
6 changed files with 16 additions and 35 deletions

View File

@@ -1,9 +0,0 @@
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

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

View File

@@ -6,8 +6,6 @@ class Proposal < ActiveRecord::Base
include Sanitizable
include Searchable
include Filterable
include HasPublicAuthor
extend HasPublicAuthorTypes
acts_as_votable
acts_as_paranoid column: :hidden_at
@@ -16,6 +14,7 @@ class Proposal < ActiveRecord::Base
RETIRE_OPTIONS = %w(duplicated started unfeasible done other)
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
belongs_to :public_author, -> { public_activity }, class_name: 'User', foreign_key: 'author_id'
belongs_to :geozone
has_many :comments, as: :commentable
has_many :proposal_notifications

View File

@@ -46,14 +46,15 @@ class User < ActiveRecord::Base
attr_accessor :skip_password_validation
attr_accessor :use_redeemable_code
scope :administrators, -> { joins(:administrators) }
scope :moderators, -> { joins(:moderator) }
scope :organizations, -> { joins(:organization) }
scope :officials, -> { where("official_level > 0") }
scope :for_render, -> { includes(:organization) }
scope :by_document, -> (document_type, document_number) { where(document_type: document_type, document_number: document_number) }
scope :email_digest, -> { where(email_digest: true) }
scope :active, -> { where(erased_at: nil) }
scope :administrators, -> { joins(:administrators) }
scope :moderators, -> { joins(:moderator) }
scope :organizations, -> { joins(:organization) }
scope :officials, -> { where("official_level > 0") }
scope :for_render, -> { includes(:organization) }
scope :by_document, -> (document_type, document_number) { where(document_type: document_type, document_number: document_number) }
scope :email_digest, -> { where(email_digest: true) }
scope :active, -> { where(erased_at: nil) }
scope :public_activity, -> { where(public_activity: true) }
before_validation :clean_document_number

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 public_author_id public_author created_at comments ],
Proposal => %I[ id title description public_author created_at comments ],
Comment => %I[ id body user_id user commentable_id ],
Organization => %I[ id name ]
}
TypeCreator = GraphQL::TypeCreator.new
type_creator = GraphQL::TypeCreator.new
API_TYPE_DEFINITIONS.each do |model, fields|
TypeCreator.create(model, fields)
type_creator.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"
TypeCreator.created_types.each do |model, created_type|
type_creator.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

@@ -26,14 +26,13 @@ module GraphQL
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])
elsif association = type_creator.class.association?(model, field_name)
else
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