Class: BrickLayer::DataSet

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::FullTextSearch, Mongoid::Timestamps
Defined in:
app/models/brick_layer/data_set.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/models/brick_layer/data_set.rb', line 208

def method_missing(*args)
  # Catch Un-initiated Attributes
  begin
    super(*args)
  rescue NoMethodError => e
    if !self.route_custom_template_class.blank?
      raise e unless self.route_custom_template_class.all_fields.include?(e.name.to_s)
    else
      Rails.logger.error("ERROR (NO METHOD): #{e}")
    end
  end
end

Class Attribute Details

.custom_field_optionsObject

Returns the value of attribute custom_field_options.



89
90
91
# File 'app/models/brick_layer/data_set.rb', line 89

def custom_field_options
  @custom_field_options
end

.custom_fieldsObject

Returns the value of attribute custom_fields.



89
90
91
# File 'app/models/brick_layer/data_set.rb', line 89

def custom_fields
  @custom_fields
end

Class Method Details

.all_fields(options = {}) ⇒ Object

Display all fields in arry for the class



53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/brick_layer/data_set.rb', line 53

def self.all_fields(options={})
  if options[:added_only]
    results = self.fields.keys - BrickLayer::DataSet.fields.keys 
  else
    results = self.fields.keys
  end
  
  results += options[:add_fields].map(&:to_s) if options[:add_fields]    
  options[:ignore_fields].each { |f| results.delete(f) } if options[:ignore_fields]
  
  return results
end

.all_fields_and_exposed_data_methodsObject

Display both fields and exposed data methods for the class



76
77
78
# File 'app/models/brick_layer/data_set.rb', line 76

def self.all_fields_and_exposed_data_methods
  (all_fields + exposed_data_methods).uniq
end

.custom_field(field_name, field_type, options = {}) ⇒ Object

This allows a user to create a custom field that will be recognized as a field type for other uses in the application like the UI



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/brick_layer/data_set.rb', line 93

def self.custom_field(field_name, field_type, options={})
  @custom_fields ||= {}
  @custom_field_options ||= {}
  
  if field_type == :image
    @custom_fields[field_name.to_sym] = :file
    
    field "#{field_name}_uid",  type: String
    field "#{field_name}_name", type: String
    
    image_accessor field_name
    
    # grab the sizes and expose it if sizes are set
    if !options[:sizes].blank?
      options[:sizes].each do |size_name, value|
        expose_data "#{field_name}_#{size_name}".to_sym
        
        define_method "#{field_name}_#{size_name}".to_sym do
          self.send(field_name.to_s).try(:thumb, value).try(:url)
        end
      end
    else
      @custom_fields[field_name.to_sym] = field_type
      field field_name, type: String
    end
  
  elsif field_type == :file
    @custom_fields[field_name.to_sym] = field_type
    field field_name, type: String
    
    mount_uploader field_name.to_sym, BrickLayer::FileUploader, :mount_on => "#{field_name}_filename".to_sym
  else
    @custom_fields[field_name.to_sym] = field_type
    field field_name, type: String
  end
  
  @custom_field_options[field_name.to_sym] = options unless options.blank?
end

.expose_data(*field_array) ⇒ Object

Expose set methods based on data set, this also gives you the ability to pull from fields that an object has set. This allows the methods to return the results with the method as the key in a hash to json conversion.

Example:

expose_data :method_1, :method_2

def method_1
  list_of_results = result_1, result_2, result_3
end


22
23
24
25
26
27
28
29
# File 'app/models/brick_layer/data_set.rb', line 22

def self.expose_data(*field_array)
  class << self
    attr_accessor :collect_exposed_data_methods
  end
  
  @collect_exposed_data_methods = @collect_exposed_data_methods.blank? ? field_array : (@collect_exposed_data_methods + field_array)
  define_method :exposed_data_methods do; self.class.collect_exposed_data_methods; end
end

.exposed_data_methodsObject

Display all exposed data methods in an array for the class



67
68
69
70
71
72
73
# File 'app/models/brick_layer/data_set.rb', line 67

def self.exposed_data_methods
  begin
    return self.new.exposed_data_methods.map(&:to_s).uniq
  rescue NoMethodError
    []
  end
end

.fulltext_search(query_string, options = {}) ⇒ Object

Override fulltext search to support inheritence



198
199
200
201
# File 'app/models/brick_layer/data_set.rb', line 198

def self.fulltext_search(query_string,options={})
  options.merge!({:type_index => self.name}) unless self.superclass == Object
  super(query_string,options)
end

.fulltext_search_in(*args) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/models/brick_layer/data_set.rb', line 179

def self.fulltext_search_in(*args)
  search_in_hash = args.find { |x| x.is_a?(Hash) }
      
  # Inject a type index filter (this allows easy support for STI full search scoping)
  if search_in_hash
    unless search_in_hash[:filters].blank?
      new_hash = search_in_hash[:filters].merge({ :type_index => lambda { |x| x._type } })
    else
      new_hash = search_in_hash.merge({:filters => { :type_index => lambda { |x| x._type } }})
    end
  end
  
  args.delete(search_in_hash) if args.include?(search_in_hash)
  
  args << (new_hash || {:filters => { :type_index => lambda { |x| x._type } }})
  super
end

Instance Method Details

#as_json(options = {}) ⇒ Object

Override as_json to return proper exposed data methods



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/brick_layer/data_set.rb', line 32

def as_json(options={})
  original_attributes = super(options)
  complete_attributes = {}
  
  # This takes a generic data sets and preps it for to json conversion
  unless self.route.blank?
    obj = self.route_custom_template_class.new(self.attributes)
  else
    obj = self
  end

  if obj.respond_to?("exposed_data_methods")
    obj.exposed_data_methods.each { |vfield| complete_attributes[vfield] = obj.send(vfield) }
  end

  complete_attributes.merge(original_attributes)
end

#route_custom_template_classObject

Grab the custom class if this class has a route



81
82
83
# File 'app/models/brick_layer/data_set.rb', line 81

def route_custom_template_class
  "PageDataSets::#{self.route.data_set_template}".constantize unless self.route.try(:data_set_template).blank?
end

#translate_obj_to_string(string_id) ⇒ Object

convert an id string to an object



148
149
150
151
152
153
154
# File 'app/models/brick_layer/data_set.rb', line 148

def translate_obj_to_string(string_id)
  begin
    BrickLayer::DataSet.find(string_id)
  rescue
    string_id
  end
end

#translate_string_ids_to_objects(array_of_ids) ⇒ Object

convert an array of ids to an array of objects



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/brick_layer/data_set.rb', line 133

def translate_string_ids_to_objects(array_of_ids)
  unless array_of_ids.blank?
    return BrickLayer::DataSet.find(array_of_ids) if array_of_ids.is_a?(String)
  
    return array_of_ids.map do |x|
      begin
        BrickLayer::DataSet.find(x)
      rescue
        x
      end
    end
  end
end

#update_attributes(update_hash) ⇒ Object

overriding #update_attributes to support the passing of object ids as an array of strings (works better for forms)



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/brick_layer/data_set.rb', line 157

def update_attributes(update_hash)
  update_hash.each do |k,v|
    if self.relations.keys.include?(k.to_s)
      self.send("#{k}=", translate_string_ids_to_objects(v)) if relations[k].macro == :referenced_in
      self.send("#{k}=", translate_obj_to_string(v))         if relations[k].macro == :references_one
      self.send("#{k}=", translate_string_ids_to_objects(v)) if relations[k].macro == :references_and_referenced_in_many || relations[k].macro == :references_many
    else
      self.send("#{k}=",v)
    end
  end

  return self.save
end