Sha256: 6184703fe47a33bd753f0bf025928c7a6304fd6ddc9bd0302ceebf4ed5383195

Contents?: true

Size: 1.35 KB

Versions: 13

Compression:

Stored size: 1.35 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 the same
      # filters_mapping(
      #   '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

13 entries across 13 versions & 1 rubygems

Version Path
praxis-2.0.pre.31 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.30 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.29 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.28 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.27 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.26 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.25 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.24 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.23 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.22 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.21 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.20 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb
praxis-2.0.pre.19 tasks/thor/templates/generator/scaffold/implementation/resources/item.rb