From 83267330a772c15bf9d9a89de7e6915b5447c67b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Thu, 26 Jan 2017 10:47:12 +0100 Subject: [PATCH] Group methods related to text generation for GraphQL documentation into a module --- app/models/comment.rb | 1 + app/models/concerns/graphqlable.rb | 32 +++++++++++++++++++++++++++++ app/models/debate.rb | 1 + app/models/geozone.rb | 3 +++ app/models/proposal.rb | 1 + app/models/proposal_notification.rb | 3 +++ app/models/user.rb | 2 ++ app/models/vote.rb | 2 ++ lib/graph_ql/api_types_creator.rb | 4 ++-- lib/graph_ql/query_type_creator.rb | 8 ++++---- 10 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 app/models/concerns/graphqlable.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 118e56b28..3c7e2c8db 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,6 +1,7 @@ class Comment < ActiveRecord::Base include Flaggable include HasPublicAuthor + include Graphqlable acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases diff --git a/app/models/concerns/graphqlable.rb b/app/models/concerns/graphqlable.rb new file mode 100644 index 000000000..0e8fdb2de --- /dev/null +++ b/app/models/concerns/graphqlable.rb @@ -0,0 +1,32 @@ +module Graphqlable + extend ActiveSupport::Concern + + class_methods do + + def graphql_field_name + self.name.gsub('::', '_').underscore.to_sym + end + + def graphql_pluralized_field_name + self.name.gsub('::', '_').underscore.pluralize.to_sym + end + + def graphql_field_description + "Find one #{self.model_name.human} by ID" + end + + def graphql_pluralized_field_description + "Find all #{self.model_name.human.pluralize}" + end + + def graphql_type_name + self.name.gsub('::', '_') + end + + def graphql_type_description + "#{self.model_name.human}" + end + + end + +end diff --git a/app/models/debate.rb b/app/models/debate.rb index ce5b63fd7..cd4b147cb 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -8,6 +8,7 @@ class Debate < ActiveRecord::Base include Searchable include Filterable include HasPublicAuthor + include Graphqlable acts_as_votable acts_as_paranoid column: :hidden_at diff --git a/app/models/geozone.rb b/app/models/geozone.rb index 7e38ce97d..55121d0ee 100644 --- a/app/models/geozone.rb +++ b/app/models/geozone.rb @@ -1,4 +1,7 @@ class Geozone < ActiveRecord::Base + + include Graphqlable + has_many :proposals has_many :spending_proposals has_many :debates diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 0cfc9bcc9..6e286104e 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -7,6 +7,7 @@ class Proposal < ActiveRecord::Base include Searchable include Filterable include HasPublicAuthor + include Graphqlable acts_as_votable acts_as_paranoid column: :hidden_at diff --git a/app/models/proposal_notification.rb b/app/models/proposal_notification.rb index 3483806c5..7faa0fec1 100644 --- a/app/models/proposal_notification.rb +++ b/app/models/proposal_notification.rb @@ -1,4 +1,7 @@ class ProposalNotification < ActiveRecord::Base + + include Graphqlable + belongs_to :author, class_name: 'User', foreign_key: 'author_id' belongs_to :proposal diff --git a/app/models/user.rb b/app/models/user.rb index 03f2db27b..e2916306c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,6 +9,8 @@ class User < ActiveRecord::Base acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases + include Graphqlable + has_one :administrator has_one :moderator has_one :valuator diff --git a/app/models/vote.rb b/app/models/vote.rb index 93bcc54be..eae53e9c3 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,5 +1,7 @@ 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"). diff --git a/lib/graph_ql/api_types_creator.rb b/lib/graph_ql/api_types_creator.rb index 03ddd5d3a..4cb3b18c4 100644 --- a/lib/graph_ql/api_types_creator.rb +++ b/lib/graph_ql/api_types_creator.rb @@ -39,8 +39,8 @@ module GraphQL created_type = GraphQL::ObjectType.define do - name(model.name) - description("#{model.model_name.human}") + name model.graphql_type_name + description model.graphql_type_description # Make a field for each column, association or method fields.each do |field_name, field_type| diff --git a/lib/graph_ql/query_type_creator.rb b/lib/graph_ql/query_type_creator.rb index 6dfde67c0..79020950e 100644 --- a/lib/graph_ql/query_type_creator.rb +++ b/lib/graph_ql/query_type_creator.rb @@ -18,16 +18,16 @@ module GraphQL query_type_creator.created_api_types.each do |model, created_type| if created_type.fields['id'] - field model.name.underscore.to_sym do + field model.graphql_field_name do type created_type - description "Find one #{model.model_name.human} by ID" + description model.graphql_field_description argument :id, !types.ID resolve -> (object, arguments, context) { model.public_for_api.find_by(id: arguments['id'])} end end - connection model.name.underscore.pluralize.to_sym, created_type.connection_type do - description "Find all #{model.model_name.human.pluralize}" + connection model.graphql_pluralized_field_name, created_type.connection_type do + description model.graphql_pluralized_field_description resolve -> (object, arguments, context) { model.public_for_api } end