Class: AdminAssistant::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/admin_assistant/model.rb

Instance Method Summary collapse

Constructor Details

#initialize(ar_model) ⇒ Model

Returns a new instance of Model.



3
4
5
# File 'lib/admin_assistant/model.rb', line 3

def initialize(ar_model)
  @ar_model = ar_model
end

Instance Method Details

#accessorsObject



7
8
9
10
11
12
# File 'lib/admin_assistant/model.rb', line 7

def accessors
  @ar_model.instance_methods.
      select { |m| m =~ /=$/ }.
      map { |m| m.gsub(/=/, '')}.
      select { |m| @ar_model.instance_methods.include?(m) }
end

#belongs_to_assoc(association_name) ⇒ Object



20
21
22
23
24
# File 'lib/admin_assistant/model.rb', line 20

def belongs_to_assoc(association_name)
  belongs_to_associations.detect { |assoc|
    assoc.name.to_s == association_name.to_s
  }
end

#belongs_to_assoc_by_foreign_key(foreign_key) ⇒ Object



26
27
28
29
30
# File 'lib/admin_assistant/model.rb', line 26

def belongs_to_assoc_by_foreign_key(foreign_key)
  belongs_to_associations.detect { |assoc|
    assoc.association_foreign_key == foreign_key
  }
end

#belongs_to_assoc_by_polymorphic_type(name) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/admin_assistant/model.rb', line 32

def belongs_to_assoc_by_polymorphic_type(name)
  if name =~ /^(.*)_type/
    belongs_to_associations.detect { |assoc|
      assoc.options[:polymorphic] && $1 == assoc.name.to_s
    }
  end
end

#belongs_to_associationsObject



14
15
16
17
18
# File 'lib/admin_assistant/model.rb', line 14

def belongs_to_associations
  @ar_model.reflect_on_all_associations.select { |assoc|
    assoc.macro == :belongs_to
  }
end

#default_column_namesObject



40
41
42
43
44
# File 'lib/admin_assistant/model.rb', line 40

def default_column_names
  @ar_model.columns.reject { |ar_column|
    %w(id created_at updated_at).include?(ar_column.name)
  }.map { |ar_column| ar_column.name }
end

#has_many_assoc(association_name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/admin_assistant/model.rb', line 46

def has_many_assoc(association_name)
  @ar_model.reflect_on_all_associations.select { |assoc|
    assoc.macro == :has_many
  }.detect { |assoc|
    assoc.name.to_s == association_name.to_s
  }
end

#paperclip_attachmentsObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/admin_assistant/model.rb', line 54

def paperclip_attachments
  pa = []
  if @ar_model.respond_to?(:attachment_definitions)
    if @ar_model.attachment_definitions
      pa = @ar_model.attachment_definitions.map { |name, definition|
        name
      }
    end
  end
  pa
end

#searchable_columnsObject



66
67
68
69
70
# File 'lib/admin_assistant/model.rb', line 66

def searchable_columns
  @ar_model.columns.select { |column|
    [:string, :text].include?(column.type)
  }
end