Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions lib/generators/perron/install_generator.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/perron/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Engine < Rails::Engine
rake_tasks do
load File.expand_path("../tasks/build.rake", __FILE__)
load File.expand_path("../tasks/clobber.rake", __FILE__)
load File.expand_path("../tasks/install.rake", __FILE__)
load File.expand_path("../tasks/sync_sources.rake", __FILE__)
load File.expand_path("../tasks/validate.rake", __FILE__)
end
Expand Down
20 changes: 20 additions & 0 deletions lib/perron/install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
say "Install Perron in your Rails app"

say "Create Perron initializer"
copy_file "#{__dir__}/install/initializer.rb", "config/initializers/perron.rb"

say "Create content data directory"
copy_file "#{__dir__}/install/README.md", "app/content/data/README.md"

say "Add Markdown gem options to Gemfile"
append_to_file "Gemfile", <<~RUBY

# Perron supports Markdown rendering using one of the following gems.
# Uncomment your preferred choice and run `bundle install`
# gem "commonmarker"
# gem "kramdown"
# gem "redcarpet"
RUBY

say "Add output folder to .gitignore"
append_to_file ".gitignore", "/#{Perron.configuration.output}/\n"
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Perron can consume structured data from YML, JSON or CSV files, making them avai

Access data sources using the `Content::Data` namespace with the class name matching the file's basename:
```erb
<%% Content::Data::Features.all.each do |feature| %>
<h4><%%= feature.name %></h4>
<p><%%= feature.description %></p>
<%% end %>
<% Content::Data::Features.all.each do |feature| %>
<h4><%= feature.name %></h4>
<p><%= feature.description %></p>
<% end %>
```

Look up a single entry with `Content::Data::Features.find("advanced-search")`, where `"advanced-search"` matches the value of the entry's `id` field.
Expand All @@ -34,15 +34,15 @@ feature[:name]

Render data collections directly using Rails-like partial rendering:
```erb
<%%= render Content::Data::Features.all %>
<%= render Content::Data::Features.all %>
```

This expects a partial at `app/views/content/features/_feature.html.erb` that will be rendered once for each item in `Content::Data::Features.all`. The individual record is made available as a local variable matching the singular form of the collection name.
```erb
<!-- app/views/content/features/_feature.html.erb -->
<div class="feature">
<h4><%%= feature.name %></h4>
<p><%%= feature.description %></p>
<h4><%= feature.name %></h4>
<p><%= feature.description %></p>
</div>
```

Expand All @@ -64,6 +64,4 @@ Data resources must contain an array of objects. Each record should include an `

## Enumerable methods

[!label v0.17.0+]

All data objects support enumerable methods like `select`, `sort_by`, `first` and `count`. See [Enumerable methods](/docs/rendering/#enumerable-methods) for the full list of available methods.
12 changes: 12 additions & 0 deletions lib/perron/tasks/install.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace :perron do
desc "Setup Perron"
task :install do
previous_location = ENV["LOCATION"]

ENV["LOCATION"] = File.expand_path("../install.rb", __dir__)

Rake::Task["app:template"].invoke

ENV["LOCATION"] = previous_location
end
end
50 changes: 0 additions & 50 deletions test/generators/perron/install_generator_test.rb

This file was deleted.

71 changes: 71 additions & 0 deletions test/perron/install_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require "test_helper"

class InstallTaskTest < ActiveSupport::TestCase
def setup
@tmpdir = File.expand_path("test/dummy/tmp/install")
@install_dir = File.expand_path("lib/perron/install")

FileUtils.mkdir_p(@tmpdir)
FileUtils.mkdir_p("#{@tmpdir}/config/initializers")
FileUtils.mkdir_p("#{@tmpdir}/app/content/data")

File.write("#{@tmpdir}/Gemfile", "source 'https://rubygems.org'\n")
File.write("#{@tmpdir}/.gitignore", "")
end

def test_creates_perron_initializer
run_template

assert File.exist?("#{@tmpdir}/config/initializers/perron.rb")
assert_match(/Perron.configure do |config|/, File.read("#{@tmpdir}/config/initializers/perron.rb"))
end

def test_creates_data_folder_with_readme
run_template

assert File.exist?("#{@tmpdir}/app/content/data/README.md")
content = File.read("#{@tmpdir}/app/content/data/README.md")

assert_match(/<% Content::Data::Features.all.each do |feature| %>/, content)
assert_match(/<%= render Content::Data::Features.all %>/, content)
end

def test_adds_markdown_gem_options_to_gemfile
run_template

content = File.read("#{@tmpdir}/Gemfile")
assert_match(/# gem "commonmarker"/, content)
assert_match(/# gem "kramdown"/, content)
assert_match(/# gem "redcarpet"/, content)
end

def test_adds_output_folder_to_gitignore
run_template

content = File.read("#{@tmpdir}/.gitignore")
assert_match(%r{/#{Perron.configuration.output}/}, content)
end

private

def run_template
Dir.chdir(@tmpdir) do
FileUtils.cp("#{@install_dir}/initializer.rb.tt", 'config/initializers/perron.rb')
FileUtils.cp("#{@install_dir}/README.md.tt", 'app/content/data/README.md')
File.open("Gemfile", "a") do |file|
file.write <<~RUBY

# Perron supports Markdown rendering using one of the following gems.
# Uncomment your preferred choice and run `bundle install`
# gem "commonmarker"
# gem "kramdown"
# gem "redcarpet"
RUBY
end

File.open(".gitignore", "a") do |file|
file.write "/#{Perron.configuration.output}/\n"
end
end
end
end
Loading