forked from refinery/refinerycms
/
singular_name.rb.erb
53 lines (45 loc) · 2.2 KB
/
singular_name.rb.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 -%>
attr_accessible <%= names_for_attr_accessible.map { |a| ":#{a}" }.join(', ') %>, :position
<% if string_attributes.any? -%>
acts_as_indexed :fields => [<%= string_attributes.map { |a| ":#{a.name}" }.join(", ") %>]
<% end -%>
<% if (text_fields = attributes.map { |a| a.name if a.type == :text }.compact.uniq).any? && text_fields.detect{ |a| a.to_s == 'message' }.nil? -%>
alias_attribute :message, :<%= text_fields.first %>
<% elsif text_fields.empty? -%>
# def message was created automatically because you didn't specify a text field
# when you ran the refinery:form generator. <3 <3 Refinery CMS.
def message
"Override def message in vendor/extensions/<%= namespacing.underscore %>/app/models/refinery/<%= namespacing.underscore %>/<%= singular_name %>.rb"
end
<% end -%>
<% unless (string_fields = attributes.map { |a| a.name if a.type == :string }.compact.uniq).empty? || 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
<% 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.underscore %>/app/models/refinery/<%= namespacing.underscore %>/<%= singular_name %>.rb"
end
<% end -%>
<% image_attributes.each do |a| -%>
belongs_to :<%= a.name -%>, :class_name => 'Refinery::Image'
<% end -%>
<% resource_attributes.each do |a| -%>
belongs_to :<%= a.name %>, :class_name => 'Refinery::Resource'
<% end -%>
<% attributes.select{ |a| /radio|select/ === a.type.to_s }.uniq.each do |a| %>
<%= a.name.pluralize.upcase %> = []
<% end -%>
end
end
end