Sha256: 2c4da2d5273114357cf4291c38037b14b25d065054c025b51798bf96812eeac1

Contents?: true

Size: 1.63 KB

Versions: 26

Compression:

Stored size: 1.63 KB

Contents

module Graphiti
  class Resource
    module Documentation
      extend ActiveSupport::Concern

      class_methods do
        def description=(val)
          @description = val
        end

        def description
          return @description if @description.present?

          if defined?(::I18n)
            desc = ::I18n.t :description,
              scope: i18n_resource_scope,
              default: nil
            desc ||= ::I18n.t :description,
              scope: i18n_type_scope,
              default: nil
          end
        end

        # @api private
        def attribute_description(attr_name)
          desc = all_attributes[attr_name][:description]
          return desc if desc.present?

          resolve_i18n_field_description(attr_name, field_type: :attributes)
        end

        # @api private
        def sideload_description(sideload_name)
          sideloads[sideload_name].description
        end

        # @api private
        def resolve_i18n_field_description(name, field_type:)
          if defined?(::I18n)
            desc = ::I18n.t :description,
              scope: [*i18n_type_scope, field_type, name],
              default: nil
            desc ||= ::I18n.t :description,
              scope: [*i18n_resource_scope, field_type, name],
              default: nil
          end
        end

        private

        def underscored_resource_name
          self.name.gsub(/Resource$/, '').underscore
        end

        def i18n_resource_scope
          [:graphiti, :resources, underscored_resource_name]
        end

        def i18n_type_scope
          [:graphiti, :types, type]
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
graphiti-1.0.rc.21 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.19 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.18 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.17 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.16 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.15 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.14 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.12 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.11 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.10 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.9 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.8 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.7 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.6 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.5 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.4 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.3 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.2 lib/graphiti/resource/documentation.rb
graphiti-1.0.rc.1 lib/graphiti/resource/documentation.rb
graphiti-1.0.beta.23 lib/graphiti/resource/documentation.rb