Module: BrickLayer::DataSetHelper

Defined in:
app/helpers/brick_layer/data_set_helper.rb

Instance Method Summary collapse

Instance Method Details

#attributes_for(klass, options = {}) ⇒ Object

This method grabs the attributes based on a class, allowing control of displaying and hiding certain fields



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
# File 'app/helpers/brick_layer/data_set_helper.rb', line 3

def attributes_for(klass,options={})
  ignored_fields  = ["_type","updated_at","created_at","route_id","_id"]
  ignored_fields += ["title","meta_keywords","meta_description"] unless klass.name.include?("PageDataSet")
  
  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

#belongs_to_form_field(form, relation) ⇒ Object

Generate belongs to form field



131
132
133
134
135
136
137
# File 'app/helpers/brick_layer/data_set_helper.rb', line 131

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

#form_field_formatter(form, attribute) ⇒ Object

This is used to automagically format the field properly based on the attribute class from the view



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/helpers/brick_layer/data_set_helper.rb', line 57

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
  form.input(attribute.to_sym, :as => type)
end

#has_many_form_field(form, relation) ⇒ Object

Generate a has many form field for a form and it’s relation



140
141
142
143
144
145
146
147
148
# File 'app/helpers/brick_layer/data_set_helper.rb', line 140

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] })

  (: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

#has_one_form_field(form, relation) ⇒ Object

Generate has_one to form field



122
123
124
125
126
127
128
# File 'app/helpers/brick_layer/data_set_helper.rb', line 122

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

#pretty_format_type(obj) ⇒ Object

This is to pretty up the format an object class



51
52
53
54
# File 'app/helpers/brick_layer/data_set_helper.rb', line 51

def pretty_format_type(obj)
  return obj.strftime("%B, %d %Y - %I:%m%p") if obj.class == Time
  return obj
end

#relation_field_formatter(form, relation) ⇒ Object

This is used to format a field with a relationship



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/helpers/brick_layer/data_set_helper.rb', line 97

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

#relations_for(klass, options = {}) ⇒ Object

This grabs the relationships of a class with option to ignore a relationship



39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/brick_layer/data_set_helper.rb', line 39

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