Created
January 12, 2009 19:13
-
-
Save HamptonMakes/46110 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class FieldBuilder < ActionView::Helpers::FormBuilder | |
| # Don't use check_box until it's modified to handle an un-check of a previous saved checked value... unchecked boxes | |
| # are not passed in params[]. | |
| # | |
| #def check_box(name, options = {}) | |
| # field name, options.merge({:class => 'checkbox'}) do |field_name, this_field, field_attributes, enabled| | |
| # @template.check_box_tag(field_name, true, (!(this_field.value.nil? || this_field.value.empty?) || options[:default_value]), field_attributes) #, :disabled => (!enabled).to_s) | |
| # end | |
| #end | |
| def select(name, options = {}) | |
| raise "Field Buidler: You must pass in an array of :select_options to form.select" unless options[:select_options] | |
| #handle :show_element option | |
| if options[:show_element] | |
| trigger_value = options[:show_element][:on].to_s.titleize | |
| if options[:show_element][:id] | |
| element_id = options[:show_element][:id].to_s | |
| onchange_js = "this.value == '#{trigger_value}' ? Element.show('#{element_id}') : Element.hide_and_blankify_inputs('#{element_id}');" | |
| elsif options[:show_element][:class] | |
| element_class = options[:show_element][:class] | |
| onchange_js = "this.value == '#{trigger_value}' ? document.getElementsByClassName('#{element_class}').each(Element.show) : document.getElementsByClassName('#{element_class}').each(Element.hide_and_blankify_inputs); " | |
| end | |
| if options[:html] | |
| options[:html].merge! :onchange => onchange_js | |
| else | |
| options[:html] = {:onchange => onchange_js} | |
| end | |
| options[:html][:disabled] = @is_disabled | |
| end | |
| select_options = options[:select_options].collect{|option| [option.to_s.titleize, option.to_s]} | |
| field name, options do |field_name, this_field, field_attributes, enabled| | |
| @template.select_tag(field_name, @template.options_for_select(select_options, (this_field.value || options[:default_value])), field_attributes.merge(:disabled => @is_disabled)) | |
| end | |
| end | |
| def province(name, options = {}) | |
| provinces = ['', 'Ontario', 'Quebec', 'Nova Scotia', 'New Brunswick', 'Manitoba', 'British Columbia', | |
| 'Prince Edward Island', 'Saskatchewan', 'Alberta', 'Newfoundland and Labrador', 'Northwest Territories', | |
| 'Yukon', 'Nunavut', 'Other'].sort | |
| select(name, options.merge(:select_options => provinces)) | |
| end | |
| def yes_or_no(name, options = {}) | |
| select_options = ["", "No", "Yes", "N/A"] | |
| select(name, options.reverse_merge(:select_options => select_options, :required => (options[:required] || true))) | |
| end | |
| def text_area(name, options = {}) | |
| field name, options do |field_name, this_field, field_attributes, enabled| | |
| @template.find_and_preserve("<div class='textarea_wrapper'>" + @template.text_area_tag(field_name, (this_field.value || options[:default_value]), field_attributes) + "</div>") | |
| end | |
| end | |
| def industry_function(name, options = {}) | |
| functions = ['', 'Record Company', 'Music Publisher', 'Artist Manager', 'Music Distributor'] | |
| select(name, options.reverse_merge(:select_options => functions, :required => true)) | |
| end | |
| def musical_category(name, options = {}) | |
| categories = ['', "Jazz", "Alternative", "Dance", "Hard Music", | |
| "Adult Contemporary", "Urban Music", "Childrens", | |
| "Roots", "Pop", "Rock", "Reggae", "World Music", | |
| "Classical", "Country", "Aboriginal"].sort | |
| select(name, options.reverse_merge(:select_options => categories, :required => true)) | |
| end | |
| def musical_category_with_electronica(name, options = {}) | |
| categories = {} | |
| ['', "Jazz", "Alternative", "Hard Music", | |
| "Adult Contemporary", "Urban Music", "Childrens", | |
| "Roots", "Pop", "Rock", "Reggae", "World Music", | |
| "Classical", "Country", "Aboriginal"].each do |cat| | |
| categories[cat] = cat | |
| end | |
| categories["Electronica"] = "Dance" | |
| select(name, options.reverse_merge(:select_options => categories, :required => true)) | |
| end | |
| def field(name, options = {}, &block) | |
| type = options[:type] | |
| form = @template.current_form | |
| this_field = form.get_field(name) | |
| changelog = this_field.get_changelog | |
| field_name = @object_name.to_s.underscore + "[update_fields][#{name}]" | |
| field_attributes = {:class => options[:class], | |
| :id => name, | |
| :required => options[:required], | |
| :format => options[:format]} | |
| field_attributes = field_attributes.merge(options[:html]) if options[:html] | |
| @is_disabled = is_disabled? || (!(can_edit? && options[:disabled] != true)) | |
| @is_readonly = options[:readonly] | |
| field_attributes.merge!((!@is_disabled && !@is_readonly) ? {} : {:readonly => true}) | |
| if block | |
| field_code = yield(field_name, this_field, field_attributes, @is_readyonly) | |
| else | |
| field_attributes[:disabled] = @is_readyonly | |
| field_code = @template.text_field_tag(field_name, (this_field.value || options[:default_value]), field_attributes) | |
| end | |
| if @template.respond_to?(:_default_label) | |
| options[:label] = @template._default_label if options[:label].nil? | |
| end | |
| unless options[:label] == false | |
| label_class = (options[:label_location] == :left ? "horizontal" : "vertical") | |
| open :label, {:class => label_class} do | |
| puts(options[:label] || name.to_s.titleize) | |
| if options[:format] | |
| #this holds the format error message, if needed | |
| open :span, '', | |
| :class => "format_notice", | |
| :id => "#{name}_format_notice", | |
| :style => "display: none;" | |
| end | |
| if options[:required] == true | |
| #this holds the required notice, if needed | |
| puts "<img src='/images/required_symbol.gif' class='required_notice' id='#{name}_required_notice' style='display: none;' />" | |
| end | |
| end | |
| end | |
| if @is_disabled | |
| puts this_field.value | |
| puts @template.hidden_field_tag(field_name, (this_field.value || options[:default_value]), field_attributes) | |
| else | |
| # This is regular functioning | |
| puts field_code | |
| end | |
| if options[:tip] && !@is_disabled | |
| open :div, options[:tip], :class => :tip, :id => "#{name}_tip" | |
| end | |
| if (@hide_changelog != true) && !changelog.empty? | |
| open :div, changelog, :class => "change_log" | |
| end | |
| end | |
| private | |
| # Things stolen from the ActionView | |
| def open(*args, &block) | |
| @template.open(*args, &block) | |
| end | |
| def puts(*args) | |
| @template.puts(*args) | |
| end | |
| def current_project | |
| if @template.current_form.is_a? Project | |
| @template.current_form | |
| else | |
| @template.current_form.project | |
| end | |
| end | |
| def can_edit? | |
| @template.can_edit? | |
| end | |
| def is_disabled? | |
| @template.instance_eval("@is_disabled") | |
| end | |
| def current_user | |
| current_project.user | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment