Sha256: 3877d72e4c40f17c6ad96195a9c86ddd9747cd89c56be124c96a67871e0302e1

Contents?: true

Size: 1.46 KB

Versions: 15

Compression:

Stored size: 1.46 KB

Contents

class JeraPushGenerator < Rails::Generators::NamedBase
  desc "This generator creates an initializer file at config/initializers"
  source_root File.expand_path("../templates", __FILE__)

  MissingModel = Class.new(Thor::Error)

  def initializer_file
    template 'jera_push.rb', 'config/initializers/jera_push.rb'
    inject_into_file 'config/initializers/assets.rb', "\nRails.application.config.assets.precompile += %w( jera_push/jera_push.css jera_push/jera_push.js )", after: '# Precompile additional assets.'
  end

  def locale_file
    copy_file '../../../../config/locale/jera_push.pt-BR.yml', 'config/locales/jera_push.pt-BR.yml'
  end

  def generate_migrations
		unless model_exists?
			raise MissingModel,
				"\n\tModel \"#{file_name.titlecase}\" doesn't exists. Please, create your Model and try again."
		end

		inject_into_file model_path, "\n\thas_many :devices, as: :pushable, class_name: 'JeraPush::Device'", after: '< ActiveRecord::Base'

    inject_into_file model_path, "\n\thas_many :devices, as: :pushable, class_name: 'JeraPush::Device'", after: '< ApplicationRecord'

    case self.behavior
    when :invoke
      generate "active_record:jera_push", file_name
    when :revoke
      Rails::Generators.invoke "active_record:jera_push", [file_name], behavior: :revoke
    end
  end

  private

  	def model_exists?
  		File.exist?(File.join(destination_root, model_path))
  	end

	  def model_path
		  @model_path ||= File.join("app", "models", "#{file_path}.rb")
		end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
jera_push-2.1.4 lib/generators/jera_push/jera_push_generator.rb
jera_push-2.1.3 lib/generators/jera_push/jera_push_generator.rb
jera_push-2.1.2 lib/generators/jera_push/jera_push_generator.rb
jera_push-2.0.0 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.2.4 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.2.3 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.2.1 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.1.7 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.1.6 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.1.5 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.1.4 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.1.3 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.1.2 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.1.1 lib/generators/jera_push/jera_push_generator.rb
jera_push-1.1.0 lib/generators/jera_push/jera_push_generator.rb