Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that there is always a 'name' field of SOME type.
- Loading branch information
Showing
1 changed file
with
24 additions
and
13 deletions.
There are no files selected for viewing
37 changes: 24 additions & 13 deletions
37
core/lib/generators/refinery/form/templates/app/models/refinery/namespace/singular_name.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,37 @@ | ||
module Refinery | ||
module <%= class_name.pluralize %> | ||
class <%= class_name %> < Refinery::Core::BaseModel | ||
<% if table_name == namespacing.underscore.pluralize -%>self.table_name = :refinery_<%= plural_name %><% end -%> | ||
acts_as_indexed :fields => [:<%= (string_fields = attributes.collect{ |a| a.name if a.type.to_s =~ /string|text/ }.compact.uniq).join(", :") %>] | ||
|
||
# Add some validation here if you want to validate the user's input<% if string_fields.any? %> | ||
<% if table_name == namespacing.underscore.pluralize -%> | ||
self.table_name = :refinery_<%= plural_name %> | ||
<% end -%> | ||
<% if (string_fields = attributes.map{ |a| a.name if a.type.to_s =~ /string|text/ }.compact.uniq).any? %> | ||
acts_as_indexed :fields => [:<%= string_fields.join(", :") %>] | ||
<% end -%> | ||
<% unless string_fields.detect{|f| f.to_s == 'name'} %> | ||
alias_attribute :name, :<%= string_fields.first %> | ||
<% end %> | ||
# Add some validation here if you want to validate the user's input | ||
<% if string_fields.any? -%> | ||
# We have validated the first string field for you. | ||
validates :<%= string_fields.first %>, :presence => true | ||
<% end -%> | ||
<% attributes.select{|a| a.type.to_s == 'image'}.uniq.each do |a| -%> | ||
<% else %> | ||
# def name was created automatically because you didn't specify a string field | ||
# when you ran the refinery:form generator. <3 <3 Refinery CMS. | ||
def name | ||
"Override def name in vendor/extensions/<%= namespacing %>/app/models/refinery/<%= namespacing %>/<%= singular_name %>.rb" | ||
end | ||
<% end -%> | ||
<% attributes.select{|a| a.type.to_s == 'image'}.uniq.each do |a| -%> | ||
belongs_to :<%= a.name.gsub("_id", "") -%>, :class_name => '::Refinery::Image' | ||
<% end -%> | ||
<% attributes.select{|a| a.type.to_s == 'resource'}.uniq.each do |a| -%> | ||
<% end -%> | ||
<% attributes.select{|a| a.type.to_s == 'resource'}.uniq.each do |a| -%> | ||
belongs_to :<%= a.name.gsub("_id", "") %>, :class_name => '::Refinery::Resource' | ||
<% end -%> | ||
<% attributes.select{|a| a.type.to_s =~ /radio|select/}.uniq.each do |a| %> | ||
<% end -%> | ||
<% attributes.select{|a| a.type.to_s =~ /radio|select/}.uniq.each do |a| -%> | ||
<%= a.name.pluralize.upcase %> = [] | ||
<% end %> | ||
<% end -%> | ||
end | ||
end | ||
end |