broke form again, but moved everything back to form helper... way nicer
Oleg
committed Oct 04, 2010
commit 84bd9b5a91b25ad7ae854297a88c3b60cad0b643
Showing 6
changed files with
46 additions
and 39 deletions
app/helpers/cms_helper.rb
+0
-30
| @@ | @@ -5,34 +5,4 @@ module CmsHelper |
| form_for(record_or_name_or_array, *(args << options.merge(:builder => CmsFormBuilder)), &proc) | |
| end | |
| - | # Helper method for all block tags. You can define your own method based |
| - | # on tag type. Example: |
| - | # tag.type = CmsTag::MyAwesomeTag |
| - | # def my_awesome_tag ... end |
| - | def cms_tag_field(tag, *args) |
| - | tag_method = tag.type.split('::').last.underscore.downcase |
| - | return send(tag_method, block) if self.respond_to? tag_method |
| - | |
| - | form_method = case tag |
| - | when CmsTag::PageText |
| - | :text_area_tag |
| - | when CmsTag::PageString |
| - | :text_field_tag |
| - | when CmsTag::PageInteger |
| - | :number_field_tag |
| - | end |
| - | |
| - | %( |
| - | <div class='form_element #{tag_method}_element'> |
| - | <div class='label'>#{tag.label.to_s.titleize}</div> |
| - | <div class='value'> |
| - | #{send(form_method, 'cms_page[cms_blocks_attributes][][content]', tag.content)} |
| - | #{hidden_field_tag('cms_page[cms_blocks_attributes][][label]', tag.label)} |
| - | #{hidden_field_tag('cms_page[cms_blocks_attributes][][type]', tag.type)} |
| - | #{hidden_field_tag('cms_page[cms_blocks_attributes][][id]', tag.id) unless tag.new_record?} |
| - | </div> |
| - | </div> |
| - | ).html_safe if form_method |
| - | end |
| - | |
| end | |
app/models/cms_page.rb
+3
-7
| @@ | @@ -51,16 +51,12 @@ class CmsPage < ActiveRecord::Base |
| cms_layout ? CmsTag.process_content(self, cms_layout.content.dup) : '' | |
| end | |
| - | def cms_tags |
| + | # Array of cms_tags for a page. Content generation is called if forced. |
| + | def cms_tags(force = false) |
| + | self.content if force |
| @cms_tags ||= [] | |
| end | |
| - | # A subset of cms_tags for form rendering |
| - | def block_cms_tags |
| - | self.content if cms_tags.blank? |
| - | cms_tags.select{ |t| t.class.superclass == CmsBlock }.uniq |
| - | end |
| - | |
| protected | |
| def assign_full_path | |
app/views/cms_admin/pages/_form_blocks.html.erb
+4
-2
| @@ | @@ -1,5 +1,7 @@ |
| <div id='form_blocks'> | |
| - | <% @cms_page.block_cms_tags.each do |tag| %> |
| - | <%= cms_tag_field(tag) %> |
| + | <%= fields_for :cms_blocks, :builder => CmsFormBuilder do |cms_blocks| %> |
| + | <% @cms_page.cms_tags(true).each do |tag| %> |
| + | <%= cms_blocks.send(tag.class.to_s.underscore.downcase.idify, tag)%> |
| + | <% end %> |
| <% end %> | |
| </div> | |
| \ No newline at end of file | |
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb
+1
-0
| @@ | @@ -1,4 +1,5 @@ |
| %w( | |
| + | comfortable_mexican_sofa/cms_rails_extensions |
| comfortable_mexican_sofa/cms_form_builder | |
| comfortable_mexican_sofa/cms_acts_as_tree | |
| ../app/models/cms_block | |
comfortable_mexican_sofa/cms_form_builder.rb b/lib/comfortable_mexican_sofa/cms_form_builder.rb
+32
-0
| @@ | @@ -29,4 +29,36 @@ class CmsFormBuilder < ActionView::Helpers::FormBuilder |
| "<label for=\"#{object_name}_#{field}\">#{label}</label>".html_safe | |
| end | |
| + | def page_text(tag) |
| + | raise 'yey' |
| + | end |
| + | |
| + | def page_string(tag) |
| + | raise 'yey' |
| + | end |
| + | |
| + | # Capturing all calls of cms_tag_* type. For those we'll try to render |
| + | # a form element. Everything else can trigger MethodNotFound error. |
| + | def method_missing(method_name, *args) |
| + | if m = method_name.to_s.match(/^cms_tag_(\w+)$/) |
| + | send(m[1], args) if respond_to?(m[1]) |
| + | else |
| + | super |
| + | end |
| + | end |
| + | |
| + | def tag_field(tag) |
| + | %( |
| + | <div class='form_element #{tag_method}_element'> |
| + | <div class='label'>#{tag.label.to_s.titleize}</div> |
| + | <div class='value'> |
| + | #{send(form_method, 'cms_page[cms_blocks_attributes][][content]', tag.content)} |
| + | #{hidden_field_tag('cms_page[cms_blocks_attributes][][label]', tag.label)} |
| + | #{hidden_field_tag('cms_page[cms_blocks_attributes][][type]', tag.type)} |
| + | #{hidden_field_tag('cms_page[cms_blocks_attributes][][id]', tag.id) unless tag.new_record?} |
| + | </div> |
| + | </div> |
| + | ) |
| + | end |
| + | |
| end | |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_rails_extensions.rb b/lib/comfortable_mexican_sofa/cms_rails_extensions.rb
+6
-0
| @@ | @@ -0,0 +1,6 @@ |
| + | class String |
| + | # Converts string to something suitable to be used as an element id |
| + | def idify |
| + | self.strip.gsub(/\W/, '_').gsub(/\s|^_*|_*$/, '').squeeze('_') |
| + | end |
| + | end |
| \ No newline at end of file | |