Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/issue_1657'
Browse files Browse the repository at this point in the history
  • Loading branch information
parndt committed Jul 9, 2012
2 parents c3c3dd7 + 73cc884 commit 30042b8
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -94,4 +94,4 @@ gem 'jquery-rails', '~> 2.0.0'
# Load local gems according to Refinery developer preference.
if File.exist?(File.expand_path('../.gemfile', __FILE__))
eval(File.read(File.expand_path('../.gemfile', __FILE__)))
end
end
Expand Up @@ -4,7 +4,7 @@ module Refinery
<% if table_name == namespacing.underscore.pluralize -%>
self.table_name = 'refinery_<%= plural_name %>'
<% end %>
attr_accessible <%= attributes.map { |a| ":#{a.name}" }.join(', ') %>, :position
attr_accessible <%= names_for_attr_accessible.map { |n| ":#{n}" }.join(', ') %>, :position
<% if localized? -%>

translates <%= localized_attributes.map { |a| ":#{a.name}" }.join(', ') %>
Expand All @@ -13,7 +13,7 @@ module Refinery
attr_accessible :locale
end
<% end -%>
<% if (string_attributes = attributes.select { |a| a.type.to_s =~ /string|text/ }.uniq).any? -%>
<% if string_attributes.any? -%>

acts_as_indexed :fields => <%= string_attributes.map {|s| s.name.to_sym }.inspect %>

Expand All @@ -26,13 +26,13 @@ module Refinery
"Override def title in vendor/extensions/<%= namespacing.underscore %>/app/models/refinery/<%= namespacing.underscore %>/<%= singular_name %>.rb"
end
<% end -%>
<% attributes.select { |a| a.type.to_s == 'image' }.uniq.each do |a| -%>
<% image_attributes.each do |a| -%>

belongs_to :<%= a.name.gsub("_id", "") -%>, :class_name => '::Refinery::Image'
belongs_to :<%= a.name -%>, :class_name => '::Refinery::Image'
<% end -%>
<% attributes.select { |a| a.type.to_s == 'resource' }.uniq.each do |a| -%>
<% resource_attributes.each do |a| -%>

belongs_to :<%= a.name.gsub("_id", "") %>, :class_name => '::Refinery::Resource'
belongs_to :<%= a.name %>, :class_name => '::Refinery::Resource'
<% end -%>
end
end
Expand Down
Expand Up @@ -6,7 +6,8 @@ module Refinery
crudify :'refinery/<%= namespacing.underscore %>/<%= singular_name %>', <% if (title = attributes.detect { |a| a.type.to_s == "string" }).present? %>
:title_attribute => "<%= title.name %>", <% end %>
:order => "created_at DESC"
<% if @includes_spam %>
<% if @includes_spam -%>

before_filter :get_spam_count, :only => [:index, :spam]

def index
Expand Down Expand Up @@ -35,7 +36,7 @@ module Refinery
def get_spam_count
@spam_count = <%= class_name %>.count(:conditions => {:spam => true})
end
<% else %>
<% else %>
def index
unless searching?
find_all_<%= plural_name %>
Expand All @@ -46,8 +47,7 @@ module Refinery
@grouped_<%= plural_name %> = group_by_date(@<%= plural_name %>)
end

<% end %>

<% end -%>
end
end
end
Expand Down
Expand Up @@ -3,24 +3,29 @@ module Refinery
class <%= class_name %> < Refinery::Core::BaseModel
<% if table_name == namespacing.underscore.pluralize -%>
self.table_name = 'refinery_<%= plural_name %>'
<% end %>
attr_accessible <%= attributes.map { |attr| ":#{attr.name}" }.join(', ') %>, :position
<% end -%>

<% if (text_or_string_fields = attributes.map{ |a| a.name if a.type.to_s =~ /string|text/ }.compact.uniq).any? -%>
acts_as_indexed :fields => [:<%= text_or_string_fields.join(", :") %>]
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.to_s == 'text'}.compact.uniq).any? && text_fields.detect{|a| a.to_s == 'message'}.nil? -%>
<% 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? %>
<% 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.to_s == 'string' }.compact.uniq).empty? || string_fields.detect{|f| f.to_s == 'name'} %>
<% 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 %>
<% 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.
Expand All @@ -32,15 +37,15 @@ module Refinery
"Override def name in vendor/extensions/<%= namespacing.underscore %>/app/models/refinery/<%= namespacing.underscore %>/<%= singular_name %>.rb"
end
<% end -%>
<% attributes.select{|a| a.type.to_s == 'image'}.uniq.each do |a| -%>
<% image_attributes.each do |a| -%>

belongs_to :<%= a.name.gsub("_id", "") -%>, :class_name => '::Refinery::Image'
belongs_to :<%= a.name -%>, :class_name => 'Refinery::Image'
<% end -%>
<% attributes.select{|a| a.type.to_s == 'resource'}.uniq.each do |a| -%>
<% resource_attributes.each do |a| -%>

belongs_to :<%= a.name.gsub("_id", "") %>, :class_name => '::Refinery::Resource'
belongs_to :<%= a.name %>, :class_name => 'Refinery::Resource'
<% end -%>
<% attributes.select{|a| a.type.to_s =~ /radio|select/}.uniq.each do |a| -%>
<% attributes.select{ |a| /radio|select/ === a.type.to_s }.uniq.each do |a| %>
<%= a.name.pluralize.upcase %> = []
<% end -%>
end
Expand Down
Expand Up @@ -6,8 +6,8 @@
<%%= render '/refinery/admin/error_messages',
:object => @<%= singular_name %>,
:include_object_name => true %>
<% string_fields = attributes.map{ |a| a.name if a.type.to_s =~ /string|text/ }.compact.uniq -%>
<% attributes.each do |attribute| %>
<% string_fields = string_attributes.map(&:name) -%>
<% attributes.each do |attribute| %>
<div class='field <%= attribute.name %>_field <%= attribute.type %>_field'>
<%%= f.<%= 'required_' if string_fields.any? && attribute.name == string_fields.first %>label :<%= attribute.name %> %>
<% case attribute.type
Expand All @@ -16,7 +16,7 @@
<% when :text -%>
<%%= f.text_area :<%= attribute.name %>, :rows => 8 %>
<% when :radio -%>
<%% <%= class_name %>::<%= attribute.name.pluralize.upcase %>.each do |value, name| %>
<%% Refinery::<%= namespacing %>::<%= class_name %>::<%= attribute.name.pluralize.upcase %>.each do |value, name| %>
<%%= f.radio_button :<%= attribute.name %>, (name || value), :value => value %>
<%%= f.label :"<%= attribute.name %>_#{(name || value)}", value.humanize %>
<%% end %>
Expand All @@ -33,7 +33,7 @@
<%%= f.time_select :<%= attribute.name %> %>
<% end -%>
</div>
<% end %>
<% end %>
<div class='actions'>
<%%= f.submit t('.send') %>
</div>
Expand Down
Expand Up @@ -5,12 +5,13 @@ class Create<%= class_name.pluralize %> < ActiveRecord::Migration
<%
attributes.each do |attribute|
# turn image or resource into what it was supposed to be which is an integer reference to an image or resource.
if attribute.type.to_s =~ /^(image|resource)$/
case attribute.type
when :image, :resource
attribute.type = 'integer'
attribute.name = "#{attribute.name}_id".gsub("_id_id", "_id")
elsif attribute.type.to_s =~ /^(radio|select)$/
attribute.name = "#{attribute.name}_id"
when :radio, :select
attribute.type = 'string'
elsif attribute.type.to_s =~ /^(checkbox)$/
when :checkbox
attribute.type = 'boolean'
end
-%>
Expand Down
23 changes: 23 additions & 0 deletions core/lib/refinery/extension_generation.rb
Expand Up @@ -63,6 +63,29 @@ def attributes_for_translation_table
localized_attributes.inject([]) {|memo, attr| memo << ":#{attr.name} => :#{attr.type}"}.join(', ')
end

def string_attributes
@string_attributes ||= attributes.select {|a| /string|text/ === a.type.to_s}.uniq
end

def image_attributes
@image_attributes ||= attributes.select { |a| a.type == :image }.uniq
end

def resource_attributes
@resource_attributes ||= attributes.select { |a| a.type == :resource }.uniq
end

def names_for_attr_accessible
@attributes_for_attr_accessible ||= attributes.map do |a|
case a.type
when :image, :resource
"#{a.name}_id" unless a.name[-3..-1] == "_id"
else
a.name
end
end
end

protected

def append_extension_to_gemfile!
Expand Down
Expand Up @@ -95,5 +95,13 @@ module Refinery
end
end
end

describe "attr_accessible" do
it "adds attributes to the list" do
File.open("#{destination_root}/vendor/extensions/rspec_product_tests/app/models/refinery/rspec_product_tests/rspec_product_test.rb") do |file|
file.grep(%r{attr_accessible :title, :description, :image_id, :brochure_id, :position}).count.should eq(1)
end
end
end
end
end

0 comments on commit 30042b8

Please sign in to comment.