Sha256: daba787cc52a98881585fccc3b97a795f1db65130430a79d403af24b81fd240d

Contents?: true

Size: 1.81 KB

Versions: 14

Compression:

Stored size: 1.81 KB

Contents

# encoding: utf-8

require "rails/generators"
require "rails/generators/rails/resource/resource_generator"

module DynamicImage
  module Generators
    class ResourceGenerator < Rails::Generators::ResourceGenerator
      desc "Creates a DynamicImage resource"

      def initialize(args, *options)
        super(inject_dynamic_image_attributes(args), *options)
      end

      def add_controller_extension
        inject_into_file(
          File.join("app/controllers",
                    class_path,
                    "#{file_name.pluralize}_controller.rb"),
          after: "ApplicationController\n"
        ) do
          "  include DynamicImage::Controller\n\n  private\n\n" \
            "  def model\n    #{class_name}\n  end\n"
        end
      end

      def add_model_extension
        inject_into_file(
          File.join("app/models", class_path, "#{file_name}.rb"),
          after: "ActiveRecord::Base\n"
        ) do
          "  include DynamicImage::Model\n"
        end
      end

      def alter_resource_routes
        gsub_file(
          File.join("config", "routes.rb"),
          " resources :#{file_name.pluralize}",
          " image_resources :#{file_name.pluralize}"
        )
      end

      private

      def inject_dynamic_image_attributes(args)
        if args.any?
          [args[0]] + dynamic_image_attributes + args[1..args.length]
        else
          args
        end
      end

      def dynamic_image_attributes
        %w(
          content_hash:string content_type:string
          content_length:integer
          filename:string
          colorspace:string
          real_width:integer real_height:integer
          crop_width:integer crop_height:integer
          crop_start_x:integer crop_start_y:integer
          crop_gravity_x:integer crop_gravity_y:integer
        )
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
dynamic_image-2.0.16 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.15 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.14 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.13 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.12 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.11 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.10 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.9 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.8 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.7 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.6 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.5 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.4 lib/rails/generators/dynamic_image/resource/resource_generator.rb
dynamic_image-2.0.3 lib/rails/generators/dynamic_image/resource/resource_generator.rb