module BrickLayer::DataSetHelper # This method grabs the attributes based on a class, allowing control of displaying and hiding certain fields def attributes_for(klass,options={}) ignored_fields = ["_type","updated_at","created_at","route_id","_id"] if klass.respond_to?("relations") ignored_fields += klass.relations.keys.map { |r| "#{r}_id" } ignored_fields += klass.relations.keys.map { |r| "#{r.singularize}_ids" } end if klass.respond_to?("all_fields") unless klass.try(:custom_fields).nil? custom_fields = klass.custom_fields.select { |k,v| v == :file } ignored_file_fields = custom_fields.keys.map { |key| "#{key}_name" } ignored_file_fields += custom_fields.keys.map { |key| "#{key}_filename" } else ignored_file_fields = [] end ignored_image_fields = klass.all_fields.select { |field| field.match(/_uid/) } ignored_fields += ignored_image_fields + ignored_file_fields image_fields = ignored_image_fields.map { |x| x.split("_uid") }.uniq fields_array = klass.all_fields(options.merge(:ignore_fields => ignored_fields)) if !image_fields.blank? && options[:ignore_image_fields] != true (fields_array + image_fields[0]) else fields_array end else [{}] end end # This grabs the relationships of a class with option to ignore a relationship def relations_for(klass, options={}) if klass.respond_to?("relations") return [] if klass.nil? result = klass.relations.keys result -= options[:ignore] if options[:ignore] else result = [] end return result end # This is to pretty up the format an object class def pretty_format_type(obj) return obj.strftime("%B, %d %Y - %I:%m%p") if obj.class == Time return obj end # This is used to automagically format the field properly based on the attribute class from the view def form_field_formatter(form,attribute) klass = form.object.class klass = "PageDataSets::#{form.object.route.data_set_template}".constantize if klass == BrickLayer::DataSet current_attribute = klass.fields[attribute].try(:type) data_type = case current_attribute.try(:to_s) when "DateTime" then :datetime when "Time" then :time when "Boolean" then :boolean when "Date" then :date when "Float" then :float when "Integer" then :integer when "BigDecimal" then :decimal when "image" then :file else if !klass.custom_fields.blank? if klass.custom_fields[attribute.to_sym].blank? :string else klass.custom_fields[attribute.to_sym] end end end data_type = :file if data_type == :image type = data_type || :string options = klass.custom_field_options[attribute.to_sym] if options && !options.keys.blank? return form.input(attribute.to_sym, options.merge(:as => type)) else return form.input(attribute.to_sym, :as => type) end rescue NameError "" end # This is used to format a field with a relationship def relation_field_formatter(form,relation) return {} if form.object.class.try(:name).try(:blank?) klass = form.object.class klass = "PageDataSets::#{form.object.route.data_set_template}".constantize if klass == BrickLayer::DataSet relation_obj = klass.relations[relation] options = { :prompt => "--- Select #{relation_obj.class_name.titlecase} ---" } options = case relation_obj.macro when :referenced_in belongs_to_form_field(form, relation) when :references_many has_many_form_field(form, relation) when :references_one has_one_form_field(form, relation) when :references_and_referenced_in_many has_many_form_field(form, relation) else {} end end # Generate has_one to form field def has_one_form_field(form, relation) klass = form.object.class relation_obj = klass.relations[relation] collection_opts = options_for_select(relation_obj.class_name.constantize.all.map { |x| [x.to_s, x.id] }, form.object.send(relation).try(:id)) form.select(relation.to_sym, collection_opts, :include_blank => "--- Select #{relation.titlecase} ---") end # Generate belongs to form field def belongs_to_form_field(form, relation) klass = form.object.class relation_obj = klass.relations[relation] collection_opts = options_for_select(relation_obj.class_name.constantize.all.map { |x| [x.to_s, x.id] }, form.object.send(relation).try(:id)) form.select(relation.to_sym, collection_opts, :include_blank => "--- Select #{relation.titlecase} ---") end # Generate a has many form field for a form and it's relation def has_many_form_field(form, relation) klass = form.object.class relation_obj = klass.relations[relation] collection_opts = options_for_select(relation_obj.class_name.constantize.all.sort_by { |x| x.to_s }.collect { |x| [x.to_s, x.id] }) content_tag(:select, :name => "[data_set][#{relation}][]", :multiple => "multiple") do options_for_select(relation_obj.class_name.constantize.all.sort_by { |x| x.to_s }.collect { |x| [x.to_s, x.id] }, form.object.send(relation).map { |x| x.id.to_s }) end end end