Sha256: a48be66c7c69a21429fa1c8357c717a6ea56b51c56ea0eceb14f6489fb5f7e8b

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module <%= version_module %>
  module Resources
    class <%= singular_class %> < Base
      model ::<%= singular_class %> # Change it if it maps to a different DB model class

      # Define the name mapping from API filter params, to model attribute/associations
      # when they aren't 1:1
      # filters_mapping(
      #   'name': 'name',
      #   'label': 'association.label_name'
      # )

      # Add dependencies for resource attributes to other attributes and/or model associations
      # property :href, dependencies: %i[id]

      <%- if action_enabled?(:create) -%>
      def self.create(payload)
        # Assuming the API field names directly map the the model attributes. Massage if appropriate.
        self.new(model.create(*payload.to_h))
      end
      <%- end -%>

      <%- if action_enabled?(:update) -%>
      def self.update(id:, payload:)
        record = model.find_by(id: id)
        return nil unless record
        # Assuming the API field names directly map the the model attributes. Massage if appropriate.
        record.update(*payload.to_h)
        self.new(record)
      end
      <%- end -%>

      <%- if action_enabled?(:delete) -%>
      def self.delete(id:)
        record = model.find_by(id: id)
        return nil unless record
        record.destroy
        self.new(record)
      end
      <%- end -%>  
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
praxis-2.0.pre.14 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.13 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.12 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.11 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb