Sha256: 760ce9311cca77b4191d4b3b9617db9e30b00cf931dff4d1495a86e08f7bc486

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

require "rails/generators/active_record/model/model_generator"

# TODO: add shoestrap to gemfile

module Shoestrap
  class CmsGenerator < ActiveRecord::Generators::ModelGenerator
    source_root File.expand_path('../templates/cms', __FILE__)
    # ... and those files are from: activerecord-3.2.11/lib/rails/generators/active_record/model/templates

    remove_hook_for :test_framework

    def create_migration_file
      return unless options[:migration] && options[:parent].nil?
      migration_template "migration.rb", "db/migrate/create_#{table_name.gsub('cms_','')}.rb"
    end

    def create_model_file
      template 'model.rb', File.join('app/models', "#{file_name}.rb")
    end

    def create_module_file
    end

    def generate_scaffolds
      generate 'scaffold_controller', "#{name} #{attributes_string} --routing-specs=false --request-specs=false --controller-specs=false --view-specs=false"
    end

    def generate_route
      generate 'resource_route', name
    end

    def generate_nav_link
      inject_into_file 'config/cms_navigation.rb', :before => 'primary.dom_class = \'nav\'' do
        "primary.item :#{real_table_name}, t('cms.navigation.items.#{real_table_name}'), #{plural_table_name}_path\n    "
      end
      inject_into_file 'config/locales/de/views/cms/shoestrap.yml', :after => 'items:' do
        "\n  #{real_table_name}:TRANSLATEME\n"
      end
    end

    def generate_translations
      template 'model.yml.erb', "config/locales/de/models/#{real_table_name}.yml"
      template 'view.yml.erb', "config/locales/de/views/cms/#{real_table_name}.yml"
    end

    protected

    def attributes_string
      attributes.map{ |a| "#{a.name}:#{a.type}" }.join(' ')
    end

    def real_table_name
      table_name.gsub('cms_', '')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shoestrap-0.4.1 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.4.0 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.3.1.pre lib/generators/shoestrap/cms_generator.rb