Sha256: 149703615cc6e1fe3666388d58bc9ceb2ab9264633783c21336c1d0fcaf136e1

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 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", [name], options.merge(migration: false)
      end

      def inject_model_methods
        if materialized? && generating?
          inject_into_class "app/models/#{file_path}.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

4 entries across 4 versions & 1 rubygems

Version Path
scenic-1.2.0 lib/generators/scenic/model/model_generator.rb
scenic-1.1.1 lib/generators/scenic/model/model_generator.rb
scenic-1.1.0 lib/generators/scenic/model/model_generator.rb
scenic-1.0.0 lib/generators/scenic/model/model_generator.rb