Sha256: 712ca2719eeedbb5763827fe1055e86c4d0e075ab1e42e3d24b15fc476c32d79

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

require "rails/generators"
require "rails/generators/rails/model/model_generator"
require "generators/scenic/view/view_generator"
require "generators/scenic/materializable"

module Scenic
  module Generators
    # @api private
    class ModelGenerator < Rails::Generators::NamedBase
      include Scenic::Generators::Materializable
      source_root File.expand_path("../templates", __FILE__)

      def invoke_rails_model_generator
        invoke "model",
          [file_path.singularize],
          options.merge(
            fixture_replacement: false,
            migration: false,
          )
      end

      def inject_model_methods
        if materialized? && generating?
          inject_into_class "app/models/#{file_path.singularize}.rb", class_name do
            evaluate_template("model.erb")
          end
        end
      end

      def invoke_view_generator
        invoke "scenic:view", [table_name], options
      end

      private

      def evaluate_template(source)
        source  = File.expand_path(find_in_source_paths(source.to_s))
        context = instance_eval("binding")
        ERB.new(
          ::File.binread(source),
          nil,
          "-",
          "@output_buffer",
        ).result(context)
      end

      def generating?
        behavior != :revoke
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
scenic-1.4.1 lib/generators/scenic/model/model_generator.rb
scenic-1.4.0 lib/generators/scenic/model/model_generator.rb