Class: Kitsune::Inspector
- Inherits:
-
Object
- Object
- Kitsune::Inspector
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
149
150
151
|
# File 'lib/kitsune/inspector.rb', line 149
def method_missing(method, *args, &block)
@object.send(method, *args, &block)
end
|
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
6
7
8
|
# File 'lib/kitsune/inspector.rb', line 6
def object
@object
end
|
Instance Method Details
#admin_name ⇒ Object
12
13
14
|
# File 'lib/kitsune/inspector.rb', line 12
def admin_name
kitsune_admin[:name] || @object.to_s.pluralize
end
|
#always?(column) ⇒ Boolean
139
140
141
142
|
# File 'lib/kitsune/inspector.rb', line 139
def always?(column)
kitsune_admin[:always] && kitsune_admin[:always].include?(column.name.to_sym)
end
|
#category ⇒ Object
24
25
26
|
# File 'lib/kitsune/inspector.rb', line 24
def category
kitsune_admin[:category] || nil
end
|
#column_linked?(column) ⇒ Boolean
144
145
146
147
|
# File 'lib/kitsune/inspector.rb', line 144
def column_linked?(column)
kitsune_admin[:linked] && kitsune_admin[:linked].include?(column.name.to_sym)
end
|
#column_sortable(column) ⇒ Object
134
135
136
137
|
# File 'lib/kitsune/inspector.rb', line 134
def column_sortable(column)
kitsune_admin[:sortable] && kitsune_admin[:sortable].include?(column.name.to_sym)
end
|
#columns(type = nil, include_pk = false) ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/kitsune/inspector.rb', line 68
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_display ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/kitsune/inspector.rb', line 94
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_edit ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/kitsune/inspector.rb', line 77
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_reflections ⇒ Object
124
125
126
127
128
129
130
131
132
|
# File 'lib/kitsune/inspector.rb', line 124
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
|
#columns_for_show ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/kitsune/inspector.rb', line 109
def columns_for_show
columns = @object.columns.dup
if kitsune_admin[:show] && kitsune_admin[:show][:fields]
columns = columns.select{|c| kitsune_admin[:show][:fields].include?(c.name.to_sym)}
column_names = columns.map{|c| c.name.to_sym}
fields_to_add = kitsune_admin[:show][: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
|
#detect_label(collection) ⇒ Object
182
183
184
185
186
187
|
# File 'lib/kitsune/inspector.rb', line 182
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
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
210
211
212
213
214
215
216
217
218
|
# File 'lib/kitsune/inspector.rb', line 210
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
252
253
254
255
|
# File 'lib/kitsune/inspector.rb', line 252
def field_type(field)
return kitsune_admin[:fields][field.to_sym][:type] if field_defined(field)
return nil
end
|
#find_association_class(column) ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/kitsune/inspector.rb', line 189
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
@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
|
220
221
222
223
224
225
226
|
# File 'lib/kitsune/inspector.rb', line 220
def form_options_for(record)
options = {:method => method_for_record(record)}
if kitsune_admin[:multipart]
options.merge!({:html => {:multipart => true}})
end
options
end
|
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/kitsune/inspector.rb', line 153
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
40
41
42
|
# File 'lib/kitsune/inspector.rb', line 40
def is_sti?
kitsune_admin[:is_sti]
end
|
#is_sti_child? ⇒ Boolean
44
45
46
47
48
49
50
|
# File 'lib/kitsune/inspector.rb', line 44
def is_sti_child?
if defined? ::ActiveRecord
@object.ancestors[1] != ::ActiveRecord::Base
else
false
end
end
|
#method_for_record(record) ⇒ Object
228
229
230
|
# File 'lib/kitsune/inspector.rb', line 228
def method_for_record(record)
record.new_record? ? :post : :put
end
|
#new_record(*args) ⇒ Object
64
65
66
|
# File 'lib/kitsune/inspector.rb', line 64
def new_record(*args)
@object.new(*args)
end
|
#object_class ⇒ Object
60
61
62
|
# File 'lib/kitsune/inspector.rb', line 60
def object_class
@object.to_s
end
|
#options_for(column) ⇒ Object
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/kitsune/inspector.rb', line 232
def options_for(column)
field = column.name
if field_type(field) == :select or field_type(field) == :collection_select
field_options(field)
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_by ⇒ Object
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_hash ⇒ Object
20
21
22
|
# File 'lib/kitsune/inspector.rb', line 20
def order_by_hash
kitsune_admin[:order_by] || nil
end
|
#run_hooks(type, *args) ⇒ Object
36
37
38
|
# File 'lib/kitsune/inspector.rb', line 36
def run_hooks(type, *args)
kitsune_admin[type.to_sym].call(*args) if kitsune_admin[type.to_sym]
end
|
#sti_column ⇒ Object
52
53
54
|
# File 'lib/kitsune/inspector.rb', line 52
def sti_column
kitsune_admin[:sti_column]
end
|
#tabs ⇒ Object
56
57
58
|
# File 'lib/kitsune/inspector.rb', line 56
def tabs
kitsune_admin[:tabs]
end
|
#versioned? ⇒ Boolean
28
29
30
|
# File 'lib/kitsune/inspector.rb', line 28
def versioned?
!!kitsune_admin[:versioned]
end
|