Skip to content

Commit

Permalink
Add ability for extension/form generators to append to existing seeds…
Browse files Browse the repository at this point in the history
….rb file instead of overwriting it. #1532.
  • Loading branch information
ugisozols committed May 23, 2012
1 parent fe9ad87 commit b7555e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/lib/generators/refinery/engine/engine_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def generate

merge_locales!

copy_or_merge_seeds!

append_extension_to_gemfile!

finalize_extension!
Expand Down
2 changes: 2 additions & 0 deletions core/lib/generators/refinery/form/form_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def generate

merge_locales!

copy_or_merge_seeds!

append_extension_to_gemfile!

finalize_extension!
Expand Down
22 changes: 22 additions & 0 deletions core/lib/refinery/extension_generation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def evaluate_templates!
reject_template?(f)
}.sort.each do |path|
if (template_path = extension_path_for(path, extension_name)).present?
next if path.to_s =~ /seeds.rb/
template path, template_path
end
end
Expand Down Expand Up @@ -209,6 +210,27 @@ def merge_locales!
end
end

def copy_or_merge_seeds!
source_seed_file = source_pathname.join("db/seeds.rb")
destination_seed_file = destination_pathname.join(extension_path_for(source_seed_file, extension_name))

if existing_extension?
# create temp seeds file
temp_seed_file = destination_pathname.join(extension_path_for("tmp/seeds.rb", extension_name))

# copy/evaluate seeds template to temp file
template source_seed_file, temp_seed_file, :verbose => false

# append temp seeds file content to extension seeds file
destination_seed_file.open('a+') { |file| file.puts temp_seed_file.read.to_s }

# remove temp file
FileUtils.rm_rf temp_seed_file
else
template source_seed_file, destination_seed_file
end
end

def puts_instructions!
unless Rails.env.test?
puts "------------------------"
Expand Down

0 comments on commit b7555e1

Please sign in to comment.