Skip to content

Commit

Permalink
Update the model generator. Fixes #665
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jan 5, 2015
1 parent 50222a5 commit 55ae466
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
14 changes: 8 additions & 6 deletions lib/generators/active_fedora/model/model_generator.rb
Expand Up @@ -5,16 +5,18 @@ class ModelGenerator < Rails::Generators::NamedBase
source_root ::File.expand_path('../templates', __FILE__)
check_class_collision

class_option :directory, :type => :string, :default => 'models', :desc => "Which directory to generate? (i.e. app/DIRECTORY)"
class_option :datastream_directory, :type => :string, :default => 'models/datastreams', :desc => "Which datastream directory to generate? (i.e. models/datastreams)"
class_option :has_file_datastream, :type => :string, :default => nil, :desc => "Name a file datastream to create"
class_option :descMetadata, :type => :string, :default => nil, :desc => "Add a descMetadata metadata datastream"
class_option :directory, type: :string, default: 'models', desc: "Which directory to generate? (i.e. app/DIRECTORY)"
class_option :datastream_directory, type: :string, default: 'models/datastreams', desc: "Which datastream directory to generate? (i.e. models/datastreams)"
class_option :contains, type: :string, default: nil, desc: "Name a file to attach"
class_option :datastream, type: :string, default: nil, desc: "Name a metadata datastream to create"

def install
template('model.rb.erb', ::File.join('app', directory, "#{file_name}.rb"))
template('datastream.rb.erb', ::File.join('app', datastream_directory, "#{file_name}_metadata.rb"))
template('model_spec.rb.erb', ::File.join('spec', directory, "#{file_name}_spec.rb"))
template('datastream_spec.rb.erb', ::File.join('spec', datastream_directory, "#{file_name}_metadata_spec.rb"))
if options[:datastream]
template('datastream.rb.erb', ::File.join('app', datastream_directory, "#{file_name}_metadata.rb"))
template('datastream_spec.rb.erb', ::File.join('spec', datastream_directory, "#{file_name}_metadata_spec.rb"))
end
end

protected
Expand Down
52 changes: 35 additions & 17 deletions lib/generators/active_fedora/model/templates/model.rb.erb
@@ -1,28 +1,46 @@
# Generated via
# `rails generate active_fedora::model <%= class_name %>`
# `rails generate active_fedora:model <%= class_name %>`
class <%= class_name %> < ActiveFedora::Base
<% if options['descMetadata'] %>
has_metadata "descMetadata", type: <%= options['descMetadata'] %>
<% if options['datastream'] %>
contains :descMetadata, class_name: "<%= options['datastream'] %>"
<% else %>
# Creating a #descMetadata method that returns the datastream.
# Define some properties to store:
#
has_metadata "descMetadata", type: <%= class_name %>Metadata
property :title, predicate: RDF::DC.title do |index|
index.as :stored_searchable, :facetable
end
property :creator, predicate: RDF::DC.creator do |index|
index.as :stored_searchable, :facetable
end
property :contributor, predicate: RDF::DC.contributor do |index|
index.as :stored_searchable, :facetable
end
property :description, predicate: RDF::DC.description do |index|
index.as :stored_searchable
end
<%- end -%>
<% if options['has_file_datastream'] %>
has_file_datastream "<%= options['has_file_datastream'] %>"
<% if options['contains'] %>
contains :<%= options['contains'] %>"
<% else %>
# Uncomment the following lines to add an #attachment method that is a
# file_datastream:
# Uncomment the following lines to add an #attachment method that is a file
#
# has_file_datastream "attachment"
# contains "attachment"
<% end %>
# "If you need to add additional attributes to the SOLR document, define the
# #to_solr method and make sure to use super"
#
# def to_solr(solr_document={}, options={})
# super(solr_document, options)
# solr_document["my_attribute_s"] = my_attribute
# return solr_document
# If you need to add additional attributes to the SOLR document, extend the default indexer:
#
# def indexer
# MyApp::IndexingService
# end
#
# This can go into app/services/my_app/indexing_service.rb
# module MyApp
# class IndexingService < ActiveFedora::IndexingService
# def generate_solr_document
# super.tap do |solr_doc|
# solr_document["my_attribute_s"] = object.my_attribute
# end
# end
# end
# end

end

0 comments on commit 55ae466

Please sign in to comment.