Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 4c9dc9b

Browse files
committed
Progress on Generators.
1 parent 14c6c3b commit 4c9dc9b

14 files changed

+532
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
module Blacklight::Sparql
3+
class Assets < Rails::Generators::Base
4+
source_root File.expand_path('../templates', __FILE__)
5+
6+
def assets
7+
copy_file "blacklight.scss", "app/assets/stylesheets/blacklight.scss"
8+
9+
return if has_blacklight_assets?
10+
11+
marker = if turbolinks?
12+
'//= require turbolinks'
13+
else
14+
'//= require jquery_ujs'
15+
end
16+
17+
insert_into_file "app/assets/javascripts/application.js", :after => marker do
18+
<<-EOF
19+
//
20+
// Required by Blacklight
21+
//= require blacklight/blacklight
22+
EOF
23+
end
24+
25+
end
26+
27+
private
28+
29+
def turbolinks?
30+
@turbolinks ||= IO.read("app/assets/javascripts/application.js").include?('turbolinks')
31+
end
32+
33+
def has_blacklight_assets?
34+
IO.read("app/assets/javascripts/application.js").include?('blacklight/blacklight')
35+
end
36+
end
37+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
module Blacklight::Sparql
3+
class ControllerGenerator < Rails::Generators::Base
4+
5+
source_root File.expand_path('../templates', __FILE__)
6+
7+
argument :controller_name , type: :string , default: "catalog"
8+
9+
desc <<-EOS
10+
This generator makes the following changes to your application:
11+
1. Injects behavior into your user application_controller.rb
12+
2. Creates a Blacklight::Catalog-based controller
13+
3. Adds routes for your controller
14+
EOS
15+
16+
# Add Blacklight to the application controller
17+
def inject_blacklight_controller_behavior
18+
inject_into_class "app/controllers/application_controller.rb", "ApplicationController" do
19+
" # Adds a few additional behaviors into the application controller\n" \
20+
" include Blacklight::Controller\n" \
21+
" layout 'blacklight'\n\n"
22+
end
23+
end
24+
25+
# Generate blacklight catalog controller
26+
def create_blacklight_catalog
27+
template "catalog_controller.rb", "app/controllers/#{controller_name}_controller.rb"
28+
end
29+
30+
def inject_blacklight_routes
31+
route <<-EOF
32+
concern :searchable, Blacklight::Routes::Searchable.new
33+
34+
resource :catalog, only: [:index], as: 'catalog', path: '/catalog', controller: 'catalog' do
35+
concerns :searchable
36+
end
37+
EOF
38+
end
39+
end
40+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
require 'rails/generators'
3+
4+
module Blacklight::Sparql
5+
class DocumentGenerator < Rails::Generators::Base
6+
include Rails::Generators::Migration
7+
8+
source_root File.expand_path('../templates', __FILE__)
9+
10+
argument :model_name, :type => :string , :default => "sparql_document"
11+
12+
desc <<-EOS
13+
This generator makes the following changes to your application:
14+
1. Creates a blacklight document in your /app/models directory
15+
EOS
16+
17+
def create_sparql_document
18+
template "sparql_document.rb", "app/models/#{model_name}.rb"
19+
end
20+
21+
end
22+
end
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# frozen_string_literal: true
2+
module Blacklight::Sparql
3+
class Install < Rails::Generators::Base
4+
5+
source_root File.expand_path('../templates', __FILE__)
6+
7+
argument :model_name , type: :string , default: "user"
8+
argument :controller_name, type: :string , default: "catalog"
9+
argument :document_name, type: :string , default: "sparql_document"
10+
argument :search_builder_name, type: :string , default: "search_builder"
11+
12+
class_option :devise , type: :boolean, default: false, aliases: "-d", desc: "Use Devise as authentication logic."
13+
14+
desc <<-EOS
15+
This generator makes the following changes to your application:
16+
1. Generates blacklight:models
17+
2. Creates a number of public assets, including images, stylesheets, and javascript
18+
3. Injects behavior into your user application_controller.rb
19+
4. Adds Blacklight routes to your ./config/routes.rb
20+
21+
Thank you for Installing SparqLight.
22+
EOS
23+
24+
def bundle_install
25+
Bundler.with_clean_env do
26+
run "bundle install"
27+
end
28+
end
29+
30+
# Copy all files in templates/public/ directory to public/
31+
# Call external generator in AssetsGenerator, so we can
32+
# leave that callable seperately too.
33+
# FIXME: Could be blacklight:assets, as there's nothing different here for SPARQL
34+
def copy_public_assets
35+
generate "blacklight:sparql:assets"
36+
end
37+
38+
def generate_blacklight_sparql_document
39+
generate 'blacklight:sparql:document', document_name
40+
end
41+
42+
def generate_search_builder
43+
generate 'blacklight:sparql:search_builder', search_builder_name
44+
end
45+
46+
def generate_blacklight_sparql_models
47+
generate 'blacklight:sparql:models'
48+
end
49+
50+
# FIXME: Could be blacklight:user, as there's nothing different here for SPARQL
51+
def generate_blacklight_user
52+
53+
generator_args = [model_name]
54+
if options[:devise]
55+
generator_args << "--devise #{options[:devise]}"
56+
end
57+
58+
generate 'blacklight:sparql:user', generator_args.join(" ")
59+
end
60+
61+
# FIXME: Could be blacklight:controller, as there's nothing different here for SPARQL
62+
def generate_controller
63+
generate 'blacklight:sparql:controller', controller_name
64+
end
65+
66+
def add_default_catalog_route
67+
route("root to: \"#{controller_name}#index\"")
68+
end
69+
70+
def add_sparqlight_configuration
71+
72+
insert_into_file "config/application.rb", :after => "require 'rails/all'" do <<-EOF
73+
74+
require 'blacklight/sparql'
75+
EOF
76+
end
77+
end
78+
79+
def add_sass_configuration
80+
81+
insert_into_file "config/application.rb", :after => "config.assets.enabled = true" do <<EOF
82+
83+
# Default SASS Configuration, check out https://github.com/rails/sass-rails for details
84+
config.assets.compress = !Rails.env.development?
85+
EOF
86+
end
87+
end
88+
89+
def inject_blacklight_i18n_strings
90+
copy_file "blacklight.en.yml", "config/locales/blacklight.en.yml"
91+
end
92+
93+
def add_routes
94+
route "mount Blacklight::Engine => '/'"
95+
end
96+
end
97+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
require 'rails/generators'
3+
require 'rails/generators/migration'
4+
5+
module Blacklight::Sparql
6+
class ModelsGenerator < Rails::Generators::Base
7+
include Rails::Generators::Migration
8+
9+
source_root File.expand_path('../templates', __FILE__)
10+
11+
desc <<-EOS
12+
This generator makes the following changes to your application:
13+
1. Creates several database migrations if they do not exist in /db/migrate
14+
2. Creates config/blacklight.yml with a default configuration
15+
EOS
16+
17+
# Copy all files in templates/config directory to host config
18+
def create_configuration_files
19+
copy_file "config/blacklight.yml", "config/blacklight.yml"
20+
end
21+
22+
23+
# Setup the database migrations
24+
def copy_migrations
25+
rake "blacklight:sparql:install:migrations"
26+
end
27+
28+
def add_routes
29+
route <<-EOF
30+
31+
concern :exportable, Blacklight::Routes::Exportable.new
32+
33+
resources :sparql_documents, only: [:show], path: '/catalog', controller: 'catalog' do
34+
concerns :exportable
35+
end
36+
37+
resources :bookmarks do
38+
concerns :exportable
39+
40+
collection do
41+
delete 'clear'
42+
end
43+
end
44+
EOF
45+
end
46+
47+
48+
end
49+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
require 'rails/generators'
3+
4+
module Blacklight::Sparql
5+
class SearchBuilderGenerator < Rails::Generators::Base
6+
include Rails::Generators::Migration
7+
8+
source_root File.expand_path('../templates', __FILE__)
9+
10+
argument :model_name, :type => :string , :default => "search_builder"
11+
12+
desc <<-EOS
13+
This generator makes the following changes to your application:
14+
1. Creates a blacklight search builder in your /app/models directory
15+
EOS
16+
def create_search_builder
17+
template "search_builder.rb", "app/models/#{model_name}.rb"
18+
end
19+
20+
end
21+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
class AlternateController < CatalogController
3+
configure_blacklight do |config|
4+
config.index.thumbnail_method = :xyz
5+
end
6+
7+
def xyz *args
8+
view_context.image_tag "asdfg"
9+
end
10+
11+
helper_method :xyz
12+
13+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
en:
2+
blacklight:
3+
application_name: 'Blacklight'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import 'bootstrap-sprockets';
2+
3+
@import 'bootstrap';
4+
5+
@import 'blacklight/blacklight';

0 commit comments

Comments
 (0)