Class: Kitsune::Inspector

Inherits:
Object
  • Object
show all
Defined in:
lib/kitsune/inspector.rb

Overview

Inspector is a proxy for adding additional methods. These should only be available within the Kitsune admin.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Inspector

Returns a new instance of Inspector.



7
8
9
10
# File 'lib/kitsune/inspector.rb', line 7

def initialize(object)
  @object = object
  @collection_label_methods = %w[to_label display_name full_name name title username login value to_s]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



120
121
122
# File 'lib/kitsune/inspector.rb', line 120

def method_missing(method, *args, &block)
  @object.send(method, *args, &block)
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



6
7
8
# File 'lib/kitsune/inspector.rb', line 6

def object
  @object
end

Instance Method Details

#admin_nameObject



12
13
14
# File 'lib/kitsune/inspector.rb', line 12

def admin_name
  kitsune_admin[:name] || @object.to_s.pluralize
end

#categoryObject



24
25
26
# File 'lib/kitsune/inspector.rb', line 24

def category
  kitsune_admin[:category] || nil
end

#column_sortable(column) ⇒ Object



115
116
117
118
# File 'lib/kitsune/inspector.rb', line 115

def column_sortable(column)
  # move to column proxy
  kitsune_admin[:sortable] && kitsune_admin[:sortable].include?(column.name.to_sym)
end

#columns(type = nil, include_pk = false) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/kitsune/inspector.rb', line 64

def columns(type = nil, include_pk = false)
  unless type
    kolumns = @object.columns.dup
  else
    kolumns = send("columns_for_#{type}") if respond_to?("columns_for_#{type}")
  end
  include_pk ? kolumns : kolumns.reject{|c| c.primary }
end

#columns_for_displayObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kitsune/inspector.rb', line 90

def columns_for_display
  columns = @object.columns.dup
  if kitsune_admin[:display] && kitsune_admin[:display][:fields]
    columns = columns.select{|c| kitsune_admin[:display][:fields].include?(c.name.to_sym)}
    column_names = columns.map{|c| c.name.to_sym}
    fields_to_add = kitsune_admin[:display][:fields].reject{|f| column_names.include?(f)}
    fields_to_add.each do |field|
      columns << Kitsune::FauxColumn.new(field, :string)
    end
    columns
  else
    columns
  end
end

#columns_for_editObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kitsune/inspector.rb', line 73

def columns_for_edit
  kolumns = @object.columns.dup
  if kitsune_admin[:no_edit] && kitsune_admin[:no_edit][:fields]
    kolumns.reject{|c| kitsune_admin[:no_edit][:fields].include?(c.name.to_sym)}
  elsif kitsune_admin[:edit] && kitsune_admin[:edit][:fields]
    kolumns = kolumns.select{|c| kitsune_admin[:edit][:fields].include?(c.name.to_sym)}
    kolumn_names = kolumns.map{|c| c.name.to_sym}
    fields_to_add = kitsune_admin[:edit][:fields].reject{|f| kolumn_names.include?(f)}
    fields_to_add.each do |field|
      kolumns << Kitsune::FauxColumn.new(field, :string)
    end
    kolumns
  else
    kolumns
  end
end

#columns_for_reflectionsObject



105
106
107
108
109
110
111
112
113
# File 'lib/kitsune/inspector.rb', line 105

def columns_for_reflections
      if kitsune_admin[:reflections] && kitsune_admin[:reflections][:fields]
kitsune_admin[:reflections][:fields].map do |field|
  Kitsune::FauxColumn.new(field, :string)
end
      else
[]
      end
end

#detect_label(collection) ⇒ Object



153
154
155
156
157
158
# File 'lib/kitsune/inspector.rb', line 153

def detect_label(collection)
  @collection_label_methods.each do |label_name|
    return label_name if [collection].flatten.first.respond_to?(label_name)
  end
  'to_s'
end

#disabled?(type) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/kitsune/inspector.rb', line 32

def disabled?(type)
  kitsune_admin[:disabled].include?(type.to_sym)
end

#display_for(record, method, *args, &block) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/kitsune/inspector.rb', line 181

def display_for(record, method, *args, &block)
  if method.to_s =~ /._id$/
    associated_record = record.send(method.to_s.gsub(/_id$/, '').to_sym, *args, &block)
    label_method = detect_label(associated_record)
    associated_record.send(label_method.to_sym)
  else
    record.send(method.to_sym, *args, &block)
  end
end

#field_type(field) ⇒ Object



223
224
225
226
# File 'lib/kitsune/inspector.rb', line 223

def field_type(field)
  return kitsune_admin[:fields][field.to_sym][:type] if field_defined(field)
  return nil
end

#find_association_class(column) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/kitsune/inspector.rb', line 160

def find_association_class(column)
  if @object.reflections[column.name.to_s.gsub(/_id$/,'').to_sym]
    if @object.reflections[column.name.to_s.gsub(/_id$/,'').to_sym].options && @object.reflections[column.name.to_s.gsub(/_id$/,'').to_sym].options[:class_name]
      @object.reflections[column.name.to_s.gsub(/_id$/,'').to_sym].options[:class_name].constantize
    else
      return column.name.to_s.gsub(/_id$/,'').classify.constantize
    end
  else
    # foreign key
    @object.reflections.each do |key, reflection|
      if reflection.options && reflection.options[:foreign_key] && reflection.options[:foreign_key] == column.name.to_s
        if reflection.options && reflection.options[:class_name]
          reflection.options[:class_name].constantize
        else
          return key.to_s.classify.constantize
        end
      end
    end
  end
end

#form_options_for(record) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/kitsune/inspector.rb', line 191

def form_options_for(record)
  options = {:method => method_for_record(record)}
  if kitsune_admin[:multipart]
    options.merge!({:html => {:multipart => true}})
  end
  options
end

#form_type(column) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/kitsune/inspector.rb', line 124

def form_type(column)
  field = column.name
  if type = field_type(column.name)
    case type
    when :sti
      if (options = field_options(field)) && options[:classes]
        :select
      else
        :text_field
      end
when :image_field
  :file_field
    else
      kitsune_admin[:fields][column.name.to_sym][:type]
    end
  elsif column.name =~ /._id$/
    :select
  else
    case column.type
      when :text then :text_area
      when :datetime then :datetime_select
      when :date then :date_select
      when :time then :time_select
      when :boolean then :check_box
      else :text_field
    end
  end
end

#is_sti?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/kitsune/inspector.rb', line 36

def is_sti?
  kitsune_admin[:is_sti]
end

#is_sti_child?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/kitsune/inspector.rb', line 40

def is_sti_child?
  if defined? ::ActiveRecord
    @object.ancestors[1] != ::ActiveRecord::Base
  else
    false
  end
end

#method_for_record(record) ⇒ Object



199
200
201
# File 'lib/kitsune/inspector.rb', line 199

def method_for_record(record)
  record.new_record? ? :post : :put
end

#new_record(*args) ⇒ Object



60
61
62
# File 'lib/kitsune/inspector.rb', line 60

def new_record(*args)
  @object.new(*args)
end

#object_classObject



56
57
58
# File 'lib/kitsune/inspector.rb', line 56

def object_class
  @object.to_s
end

#options_for(column) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/kitsune/inspector.rb', line 203

def options_for(column)
  
  field = column.name
  
  if field_type(field) == :select
    [field_options(field), {:include_blank => true}]
  elsif column.name =~ /._id$/
    id = :id
    collection = find_association_class(column).all
    name = detect_label(collection)
    [collection.map { |element| [ element.send(name), element.send(id) ] }, {:include_blank => true}]
  elsif field_type(field) == :sti && (options = field_options(field)) && options[:classes]
    [options[:classes].map {|element| [element.to_s, element.to_s]}, {:include_blank => true}]
  else
    options = field_options(field) || {}
    options[:size] = '80x10' if [:text_area, :wysiwyg].include?(form_type(column))
    [options]
  end
end

#order_byObject



16
17
18
# File 'lib/kitsune/inspector.rb', line 16

def order_by
   order_by_hash ? order_by_hash.map{|k,v| "#{k} #{v}"}.join(' ') : nil
end

#order_by_hashObject



20
21
22
# File 'lib/kitsune/inspector.rb', line 20

def order_by_hash
  kitsune_admin[:order_by] || nil
end

#sti_columnObject



48
49
50
# File 'lib/kitsune/inspector.rb', line 48

def sti_column
  kitsune_admin[:sti_column]
end

#tabsObject



52
53
54
# File 'lib/kitsune/inspector.rb', line 52

def tabs
  kitsune_admin[:tabs]
end

#versioned?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/kitsune/inspector.rb', line 28

def versioned?
  !!kitsune_admin[:versioned]
end