Class: Section

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/section.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.model_namesObject

returns array of model names found in app/models/



18
19
20
# File 'app/models/section.rb', line 18

def self.model_names
  Dir.glob(Rails.root.to_s + '/app/models/*.rb').collect { |file| File.basename(file, '.*') }
end

.valid_model_name?(name) ⇒ Boolean

check if given model name matches existing model

Returns:

  • (Boolean)


23
24
25
# File 'app/models/section.rb', line 23

def self.valid_model_name?(name)
  !name.blank? && self.model_names.include?(name.downcase)
end

Instance Method Details

#column_namesObject

returns array of valid column names of associated model



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

def column_names
  names = []
  model.column_names.each do |col_name|
    # for paperclip attachments, add base name
    if col_name.match(/(.*)_file_name$/) 
      names << $1 
    end
    names << col_name
  end
  return names
end

#detail_fieldsObject



40
41
42
# File 'app/models/section.rb', line 40

def detail_fields
  self.fields.where(:display_type => 'detail')
end

#has_many_fieldsObject



44
45
46
# File 'app/models/section.rb', line 44

def has_many_fields
  self.fields.where(:display_type => 'has_many')
end

#modelObject

returns model corresponding to model_name, or nil if not found



32
33
34
35
36
37
38
# File 'app/models/section.rb', line 32

def model
  begin
    return Object.const_get(model_name.singularize.camelize)
  rescue NameError
    return nil
  end
end

#model_countObject



65
66
67
# File 'app/models/section.rb', line 65

def model_count
   model.count
end

#name=(name_string) ⇒ Object



27
28
29
# File 'app/models/section.rb', line 27

def name=(name_string)
  write_attribute(:name, name_string.gsub(/ /, "_").underscore)
end

#overview_fieldsObject



48
49
50
# File 'app/models/section.rb', line 48

def overview_fields
  self.fields.where(:display_type => 'overview')
end