Skip to content

Commit

Permalink
build a rails generator to encapsulate setup logic
Browse files Browse the repository at this point in the history
* currently only appends to gitignore
  • Loading branch information
agrobbin committed Jun 11, 2014
1 parent df477aa commit 68ea4cf
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Add the gem to your gemfile
gem 'spritely'
```

Add the following to your gitignore
Run the generator to update your gitignore

```
app/assets/images/sprites
```bash
bundle exec rails generate spritely:install
```

This directory will be created in your deployed environments when precompiling assets.
Expand Down
12 changes: 12 additions & 0 deletions lib/generators/spritely/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rails/generators/base'

module Spritely
module Generators
class InstallGenerator < ::Rails::Generators::Base
def add_sprites_folder_to_gitignore
create_file '.gitignore', skip: true
inject_into_file '.gitignore', "\n#{Spritely.relative_folder_path}\n", after: /\z/
end
end
end
end
6 changes: 5 additions & 1 deletion lib/spritely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def self.environment
end

def self.directory
::Rails.root.join('app', 'assets', 'images', 'sprites')
::Rails.root.join(relative_folder_path)
end

def self.relative_folder_path
Pathname.new(File.join('app', 'assets', 'images', 'sprites'))
end
end
19 changes: 19 additions & 0 deletions spec/generators/spritely/install_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spec_helper'
require 'generators/spritely/install_generator'

describe Spritely::Generators::InstallGenerator, :generator do
destination File.expand_path("../../../../tmp", __FILE__)

before do
prepare_destination
run_generator
end

subject { destination_root }

it { is_expected.to have_structure {
file '.gitignore' do
contains 'app/assets/images/sprites'
end
} }
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rspec'
require 'rspec/its'
require 'generator_spec/generator_example_group'
require 'spritely'

require 'pry-byebug'
Expand All @@ -12,5 +13,6 @@
end

config.include RailsAppHelpers, :integration
config.include GeneratorSpec::GeneratorExampleGroup, :generator
config.around(:each, :integration) { |example| within_rails_app(&example) }
end
8 changes: 6 additions & 2 deletions spec/spritely_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
end

describe '.directory' do
before { stub_const('::Rails', double(root: File)) }
before { stub_const('::Rails', double(root: Pathname.new('foo/bar'))) }

its(:directory) { should eq('app/assets/images/sprites') }
its(:directory) { should eq(Pathname.new('foo/bar/app/assets/images/sprites')) }
end

describe '.relative_folder_path' do
its(:relative_folder_path) { should eq(Pathname.new('app/assets/images/sprites')) }
end
end
4 changes: 2 additions & 2 deletions spec/support/rails_app_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module RailsAppHelpers
'--skip-active-record',
'--skip-test-unit',
'--skip-javascript',
'--skip-spring',
'--skip-git'
'--skip-spring'
]

def within_rails_app(&block)
Expand All @@ -19,6 +18,7 @@ def within_rails_app(&block)
f.write("gem 'spritely', path: '#{__dir__}/../../'\n")
end
%x(bundle install)
%x(rails generate spritely:install)
FileUtils.cp_r "#{__dir__}/../fixtures/rails-app/.", "."
yield
end
Expand Down
1 change: 1 addition & 0 deletions spritely.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec', '>= 3.0'
s.add_development_dependency 'rspec-its'
s.add_development_dependency 'appraisal'
s.add_development_dependency 'generator_spec'
end

0 comments on commit 68ea4cf

Please sign in to comment.