Skip to content

Commit

Permalink
Added attachment_fu model and migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjstankus committed Aug 8, 2008
1 parent 061673a commit 5423c07
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Aftfilr task list

* Class collision detection.
22 changes: 19 additions & 3 deletions generators/aftfilr/aftfilr_generator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AftfilrGenerator < Rails::Generator::NamedBase
default_options :with_categories => false
default_options :with_categories => false,:skip_migration => false

attr_reader :controller_name,
:controller_class_path,
Expand Down Expand Up @@ -35,8 +35,10 @@ def initialize(runtime_args, runtime_options = {})

def manifest
record do |m|
# Check for class collisions
m.class_collisions class_path, model_class_name #, controller_class_name
# APPTODO: Check for class collisions

# Models
m.template 'models/attfu_model.rb', "app/models/#{singular_name}.rb"

# TinyMCE plugin
m.directory(tinymce_plugin_dir)
Expand All @@ -52,6 +54,14 @@ def manifest
m.directory(File.join(tinymce_plugin_dir, 'langs'))
m.template 'tinymce_plugin/langs/en.js', File.join(tinymce_plugin_dir, 'langs', "en.js")
m.template 'tinymce_plugin/langs/en_dlg.js', File.join(tinymce_plugin_dir, 'langs', "en_dlg.js")

# Migrations
unless options[:skip_migration]
m.migration_template 'migrations/attfu_migration.rb', 'db/migrate',
:assigns => { :migration_class_name => migration_class_name },
:migration_file_name => "create_#{table_name}"
end

end
end

Expand All @@ -73,11 +83,17 @@ def controller_index_path
class_path.empty? ? "/#{plural_name}" : "/#{class_path.join('/')}/#{plural_name}"
end

def migration_class_name
"Create#{controller_class_name}"
end

protected

def add_options!(opt)
opt.separator ''
opt.separator 'Options:'
opt.on("--skip-migration",
"Do not generate migrations for any models.") { |v| options[:skip_migration] = v }
opt.on("--with-categories",
"Add categories to be associated with the generated model.") { |v| options[:with_categories] = v }
end
Expand Down
17 changes: 17 additions & 0 deletions generators/aftfilr/templates/migrations/attfu_migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class <%= migration_class_name %> < ActiveRecord::Migration
def self.up
create_table "<%= table_name %>", :force => true do |t|
t.integer :size
t.string :content_type
t.string :filename
<%- if options[:with_categories] -%>
t.integer <%= ":#{singular_name}_category_id" %>
<%- end -%>
t.timestamps
end
end
def self.down
drop_table "<%= table_name %>"
end
end
3 changes: 3 additions & 0 deletions generators/aftfilr/templates/models/attfu_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class <%= model_class_name %> < ActiveRecord::Base
has_attachment :storage => :file_system
end
6 changes: 6 additions & 0 deletions test/aftfilr_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ def test_generated_names
assert_equal 'document', g.singular_name
assert_equal 'document', g.file_name # aliased to singular_name
assert_equal 'documents', g.plural_name
assert_equal 'documents', g.table_name
assert_equal [], g.class_path
assert_equal 'Document', g.class_name
assert_equal 'Document', g.model_class_name
assert_equal 'Documents', g.controller_class_name
assert_equal 'DocumentDialog', g.tinymce_dialog_name
assert_equal 'Document Manager', g.tinymce_window_title
assert_equal '/documents', g.controller_index_path
assert_equal 'CreateDocuments', g.migration_class_name
end

def test_generated_names_for_capitalized_arg
g = build_generator('aftfilr', %w(Document))
assert_equal 'Document', g.name
assert_equal 'document', g.singular_name
assert_equal 'documents', g.plural_name
assert_equal 'documents', g.table_name
assert_equal [], g.class_path
assert_equal 'Document', g.class_name
assert_equal 'Document Manager', g.tinymce_window_title
Expand All @@ -38,6 +41,7 @@ def test_generated_names_for_camelcased_arg
assert_equal 'GeologyDocument', g.name
assert_equal 'geology_document', g.singular_name
assert_equal 'geology_documents', g.plural_name
assert_equal 'geology_documents', g.table_name
assert_equal [], g.class_path
assert_equal 'GeologyDocument', g.class_name
assert_equal 'GeologyDocuments', g.controller_class_name
Expand All @@ -51,6 +55,7 @@ def test_generated_namespaced_names_with_path_arg
assert_equal 'admin/document', g.name
assert_equal 'document', g.singular_name
assert_equal 'documents', g.plural_name
assert_equal 'admin_documents', g.table_name
assert_equal %w(admin), g.class_path
assert_equal 'Admin::Document', g.class_name
assert_equal 'Admin::Documents', g.controller_class_name
Expand All @@ -65,6 +70,7 @@ def test_generated_namespaced_names_with_scoped_arg
assert_equal 'Admin::Document', g.name
assert_equal 'document', g.singular_name
assert_equal 'documents', g.plural_name
assert_equal 'admin_documents', g.table_name
assert_equal %w(admin), g.class_path
assert_equal 'Admin::Document', g.class_name
assert_equal 'Admin::Documents', g.controller_class_name
Expand Down

0 comments on commit 5423c07

Please sign in to comment.