Sha256: bd31d521bc0e044402152316d3e5d1be5780ce0429e2b6e03650426659162aab

Contents?: true

Size: 1.77 KB

Versions: 163

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Rails
  module Generators
    # ActiveModel is a class to be implemented by each ORM to allow Rails to
    # generate customized controller code.
    #
    # The API has the same methods as ActiveRecord, but each method returns a
    # string that matches the ORM API.
    #
    # For example:
    #
    #   ActiveRecord::Generators::ActiveModel.find(Foo, "params[:id]")
    #   # => "Foo.find(params[:id])"
    #
    #   DataMapper::Generators::ActiveModel.find(Foo, "params[:id]")
    #   # => "Foo.get(params[:id])"
    #
    # On initialization, the ActiveModel accepts the instance name that will
    # receive the calls:
    #
    #   builder = ActiveRecord::Generators::ActiveModel.new "@foo"
    #   builder.save # => "@foo.save"
    #
    # The only exception in ActiveModel for ActiveRecord is the use of self.build
    # instead of self.new.
    #
    class ActiveModel
      attr_reader :name

      def initialize(name)
        @name = name
      end

      # GET index
      def self.all(klass)
        "#{klass}.all"
      end

      # GET show
      # GET edit
      # PATCH/PUT update
      # DELETE destroy
      def self.find(klass, params = nil)
        "#{klass}.find(#{params})"
      end

      # GET new
      # POST create
      def self.build(klass, params = nil)
        if params
          "#{klass}.new(#{params})"
        else
          "#{klass}.new"
        end
      end

      # POST create
      def save
        "#{name}.save"
      end

      # PATCH/PUT update
      def update(params = nil)
        "#{name}.update(#{params})"
      end

      # POST create
      # PATCH/PUT update
      def errors
        "#{name}.errors"
      end

      # DELETE destroy
      def destroy
        "#{name}.destroy"
      end
    end
  end
end

Version data entries

163 entries across 151 versions & 11 rubygems

Version Path
railties-7.0.8.6 lib/rails/generators/active_model.rb
railties-6.1.7.10 lib/rails/generators/active_model.rb
railties-6.1.7.9 lib/rails/generators/active_model.rb
railties-7.0.8.5 lib/rails/generators/active_model.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/railties-7.0.8.4/lib/rails/generators/active_model.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/active_model.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/active_model.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/active_model.rb
railties-7.0.8.4 lib/rails/generators/active_model.rb
railties-6.1.7.8 lib/rails/generators/active_model.rb
railties-7.0.8.1 lib/rails/generators/active_model.rb
railties-6.1.7.7 lib/rails/generators/active_model.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-7.0.2.3/lib/rails/generators/active_model.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-7.0.3.1/lib/rails/generators/active_model.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-6.1.6.1/lib/rails/generators/active_model.rb
railties-7.0.8 lib/rails/generators/active_model.rb
railties-7.0.7.2 lib/rails/generators/active_model.rb
railties-6.1.7.6 lib/rails/generators/active_model.rb
railties-7.0.7.1 lib/rails/generators/active_model.rb
railties-6.1.7.5 lib/rails/generators/active_model.rb