Sha256: c442109b399a91e946b2f607ca6c217c8ce96c1027f30e680d0225f3d82c7182

Contents?: true

Size: 1.74 KB

Versions: 10

Compression:

Stored size: 1.74 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
        "#{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

10 entries across 10 versions & 1 rubygems

Version Path
shoestrap-0.3.0 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.3.0.pre2 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.3.0.pre1 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.3.0.pre lib/generators/shoestrap/cms_generator.rb
shoestrap-0.2.5 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.2.4 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.2.3 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.2.2 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.2.1 lib/generators/shoestrap/cms_generator.rb
shoestrap-0.2.0 lib/generators/shoestrap/cms_generator.rb