admin_assistant 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/README +3 -28
  2. data/Rakefile +18 -10
  3. data/init.rb +1 -0
  4. data/install.rb +5 -1
  5. data/lib/admin_assistant/active_record_column.rb +211 -0
  6. data/lib/admin_assistant/association_target.rb +35 -0
  7. data/lib/admin_assistant/belongs_to_column.rb +186 -0
  8. data/lib/admin_assistant/builder.rb +222 -35
  9. data/lib/admin_assistant/column.rb +266 -297
  10. data/lib/admin_assistant/default_search_column.rb +41 -0
  11. data/lib/admin_assistant/file_column_column.rb +73 -0
  12. data/lib/admin_assistant/form_view.rb +7 -68
  13. data/lib/admin_assistant/helper.rb +4 -2
  14. data/lib/admin_assistant/index.rb +101 -81
  15. data/lib/admin_assistant/paperclip_column.rb +45 -0
  16. data/lib/admin_assistant/polymorphic_belongs_to_column.rb +102 -0
  17. data/lib/admin_assistant/request/autocomplete.rb +47 -0
  18. data/lib/admin_assistant/request/base.rb +176 -0
  19. data/lib/admin_assistant/request/create.rb +27 -0
  20. data/lib/admin_assistant/request/destroy.rb +15 -0
  21. data/lib/admin_assistant/request/edit.rb +11 -0
  22. data/lib/admin_assistant/request/index.rb +26 -0
  23. data/lib/admin_assistant/request/new.rb +19 -0
  24. data/lib/admin_assistant/request/show.rb +24 -0
  25. data/lib/admin_assistant/request/update.rb +44 -0
  26. data/lib/admin_assistant/search.rb +82 -0
  27. data/lib/admin_assistant/show_view.rb +20 -0
  28. data/lib/admin_assistant/virtual_column.rb +61 -0
  29. data/lib/admin_assistant.rb +190 -85
  30. data/lib/javascripts/admin_assistant.js +253 -0
  31. data/lib/stylesheets/activescaffold.css +219 -0
  32. data/lib/stylesheets/default.css +119 -0
  33. data/lib/views/_polymorphic_field_search.html.erb +89 -0
  34. data/lib/views/_restricted_autocompleter.html.erb +53 -0
  35. data/lib/views/autocomplete.html.erb +11 -0
  36. data/lib/views/form.html.erb +6 -3
  37. data/lib/views/index.html.erb +53 -46
  38. data/lib/views/show.html.erb +19 -0
  39. data/vendor/ar_query/MIT-LICENSE +20 -0
  40. data/vendor/ar_query/README +0 -0
  41. data/vendor/ar_query/init.rb +1 -0
  42. data/vendor/ar_query/install.rb +1 -0
  43. data/vendor/ar_query/lib/ar_query.rb +137 -0
  44. data/vendor/ar_query/spec/ar_query_spec.rb +253 -0
  45. data/vendor/ar_query/tasks/ar_query_tasks.rake +0 -0
  46. data/vendor/ar_query/uninstall.rb +1 -0
  47. metadata +39 -16
  48. data/lib/admin_assistant/request.rb +0 -183
  49. data/lib/stylesheets/admin_assistant.css +0 -75
@@ -5,6 +5,14 @@ class AdminAssistant
5
5
  def initialize(admin_assistant)
6
6
  @admin_assistant = admin_assistant
7
7
  end
8
+
9
+ def [](column_name)
10
+ ColumnConfigLookup.new(
11
+ column_name,
12
+ [@admin_assistant.base_settings, form, index, show, index.search],
13
+ @admin_assistant
14
+ )
15
+ end
8
16
 
9
17
  def actions(*a)
10
18
  if a.empty?
@@ -14,28 +22,114 @@ class AdminAssistant
14
22
  end
15
23
  end
16
24
 
17
- def inputs
18
- @admin_assistant.form_settings.inputs
19
- end
20
-
21
- def label(column, label)
22
- @admin_assistant.custom_column_labels[column.to_s] = label
25
+ def destroy(&block)
26
+ @admin_assistant.custom_destroy = block
23
27
  end
24
28
 
25
29
  def form
26
- yield @admin_assistant.form_settings
30
+ f = @admin_assistant.form_settings
31
+ block_given? ? yield(f) : f
27
32
  end
28
33
 
29
34
  def index
30
- yield @admin_assistant.index_settings
35
+ i = @admin_assistant.index_settings
36
+ block_given? ? yield(i) : i
37
+ end
38
+
39
+ def model_class_name=(mcn)
40
+ @admin_assistant.model_class_name = mcn
41
+ end
42
+
43
+ def show
44
+ s = @admin_assistant.show_settings
45
+ block_given? ? yield(s) : s
46
+ end
47
+
48
+ class ColumnConfigLookup
49
+ def initialize(column_name, settingses, admin_assistant)
50
+ @column_name, @settingses, @admin_assistant =
51
+ column_name, settingses, admin_assistant
52
+ end
53
+
54
+ def method_missing(meth, *args)
55
+ match = meth
56
+ if match.to_s =~ /^(.*)=$/
57
+ match = $1.to_sym
58
+ elsif match.to_s =~ /^(.*)\?$/
59
+ match = $1.to_sym
60
+ end
61
+ setting = @settingses.detect { |setting|
62
+ setting.column_config_args.keys.include?(match)
63
+ }
64
+ if setting
65
+ setting[@column_name].send(meth, *args)
66
+ else
67
+ super
68
+ end
69
+ end
31
70
  end
32
71
  end
33
72
 
34
- class Settings
73
+ class ColumnConfig
74
+ def initialize(fields_config)
75
+ @fields_config = fields_config
76
+ @values = {}
77
+ end
78
+
79
+ def method_missing_matching_fields_config(meth, block, *args)
80
+ field_type = @fields_config[meth]
81
+ if field_type == :accessor
82
+ @values[meth]
83
+ elsif field_type == :boolean
84
+ @values[meth] = true
85
+ elsif field_type == :block
86
+ if block
87
+ @values[meth] = block
88
+ else
89
+ @values[meth]
90
+ end
91
+ end
92
+ end
93
+
94
+ def find_field_from_method(meth, target_field_type, &block)
95
+ @fields_config.detect { |fn, ft|
96
+ meth.to_s =~ block.call(fn) && ft == target_field_type
97
+ }.first
98
+ end
99
+
100
+ def method_missing_maybe_setter(meth, *args)
101
+ field_name = find_field_from_method(meth, :accessor) { |fn| /^#{fn}=$/ }
102
+ @values[field_name] = args.first if field_name
103
+ end
104
+
105
+ def method_missing(meth, *args, &block)
106
+ if @fields_config[meth]
107
+ return method_missing_matching_fields_config(meth, block, *args)
108
+ elsif meth.to_s =~ /=$/
109
+ result = method_missing_maybe_setter(meth, *args)
110
+ return result unless result.nil?
111
+ elsif meth.to_s =~ /\?$/
112
+ field_name = find_field_from_method(meth, :boolean) { |fn|
113
+ /^#{fn}\?$/
114
+ }
115
+ return !@values[field_name].nil? if field_name
116
+ end
117
+ super
118
+ end
119
+ end
120
+
121
+ class AbstractSettings
35
122
  attr_reader :column_names
36
123
 
37
124
  def initialize(admin_assistant)
38
125
  @admin_assistant = admin_assistant
126
+ @column_configs = HashWithIndifferentAccess.new { |h, k|
127
+ h[k] = ColumnConfig.new(column_config_args)
128
+ }
129
+ end
130
+
131
+ def [](column_name)
132
+ @column_configs[column_name]
39
133
  end
40
134
 
41
135
  def columns(*args)
@@ -43,62 +137,155 @@ class AdminAssistant
43
137
  end
44
138
  end
45
139
 
46
- class FormSettings < Settings
47
- attr_reader :inputs, :submit_buttons
140
+ class BaseSettings < AbstractSettings
141
+ def all_polymorphic_types
142
+ @column_configs.values.map(&:polymorphic_types).flatten.compact
143
+ end
144
+
145
+ def column_config_args
146
+ {:boolean_labels => :accessor, :label => :accessor,
147
+ :polymorphic_types => :accessor}
148
+ end
149
+ end
150
+
151
+ class FormSettings < AbstractSettings
152
+ attr_reader :submit_buttons
48
153
 
49
154
  def initialize(admin_assistant)
50
155
  super
51
- @inputs = {}
52
156
  @submit_buttons = []
53
- @read_only = []
54
157
  end
55
158
 
56
- def read_only(*args)
159
+ def column_config_args
160
+ {:clear_link => :accessor, :datetime_select_options => :accessor,
161
+ :date_select_options => :accessor, :default => :block,
162
+ :description => :accessor, :exclude_blank => :boolean,
163
+ :image_size => :accessor, :input => :accessor,
164
+ :read_only => :boolean, :select_options => :accessor,
165
+ :text_area_options => :accessor, :write_once => :boolean
166
+ }
167
+ end
168
+
169
+ def columns_for_edit(*args)
170
+ if args.empty?
171
+ @columns_for_edit || column_names
172
+ else
173
+ @columns_for_edit = args
174
+ end
175
+ end
176
+
177
+ def columns_for_new(*args)
57
178
  if args.empty?
58
- @read_only
179
+ @columns_for_new || column_names
59
180
  else
60
- args.each do |arg| @read_only << arg.to_s; end
181
+ @columns_for_new = args
61
182
  end
62
183
  end
184
+
185
+ def column_names
186
+ @column_names || @admin_assistant.default_column_names
187
+ end
63
188
  end
64
189
 
65
- class IndexSettings < Settings
66
- attr_reader :actions, :link_to_args, :search_fields, :sort_by
67
- attr_accessor :total_entries
190
+ class IndexSettings < AbstractSettings
191
+ attr_reader :actions, :right_column_links, :search_settings, :sort_by
68
192
 
69
193
  def initialize(admin_assistant)
70
194
  super
71
- @actions = {}
72
- @sort_by = 'id desc'
73
- @boolean_labels = {}
74
- @link_to_args = {}
195
+ @actions = ActiveSupport::OrderedHash.new
196
+ @right_column_links = []
75
197
  @search_fields = []
198
+ @search_settings = SearchSettings.new @admin_assistant
199
+ @sort_by = "#{admin_assistant.model_class.table_name}.id desc"
76
200
  end
77
201
 
78
- def boolean_labels(*args)
79
- if args.size == 1
80
- args.first.each do |column_name, pairs|
81
- @boolean_labels[column_name.to_s] = pairs
82
- end
83
- else
84
- @boolean_labels
202
+ def column_config_args
203
+ {:image_size => :accessor, :link_to_args => :block,
204
+ :sort_by => :accessor}
205
+ end
206
+
207
+ def conditions(str = nil, &block)
208
+ if str.nil? && block.nil?
209
+ @conditions
210
+ elsif str
211
+ @conditions = str
212
+ elsif block
213
+ @conditions = block
85
214
  end
86
215
  end
87
216
 
88
- def conditions(&block)
89
- block ? (@conditions = block) : @conditions
217
+ def header(&block)
218
+ block ? (@header = block) : @header
90
219
  end
91
220
 
92
- def search(*fields)
93
- @search_fields = fields
221
+ def include(*associations)
222
+ if associations.empty?
223
+ @include
224
+ else
225
+ @include = associations
226
+ end
227
+ end
228
+
229
+ def search(*columns)
230
+ if block_given?
231
+ yield @search_settings
232
+ elsif columns.empty?
233
+ @search_settings
234
+ else
235
+ @search_settings.columns *columns
236
+ end
94
237
  end
95
238
 
96
239
  def sort_by(*sb)
97
240
  if sb.empty?
98
241
  @sort_by
99
242
  else
100
- @sort_by = sb
243
+ @sort_by = sb.first
101
244
  end
102
245
  end
246
+
247
+ def total_entries(&block)
248
+ block ? (@total_entries = block) : @total_entries
249
+ end
250
+
251
+ class SearchSettings < AbstractSettings
252
+ attr_reader :column_names
253
+
254
+ def initialize(admin_assistant)
255
+ super
256
+ @column_names = []
257
+ @default_search_includes = []
258
+ end
259
+
260
+ def column_config_args
261
+ {:blank_checkbox => :accessor, :comparators => :accessor,
262
+ :conditions => :block, :field_type => :accessor,
263
+ :match_text_fields_for_association => :boolean}
264
+ end
265
+
266
+ def columns(*c)
267
+ @column_names = c
268
+ end
269
+
270
+ def default_search_includes(*includes)
271
+ if includes.empty?
272
+ @default_search_includes
273
+ else
274
+ @default_search_includes = includes
275
+ end
276
+ end
277
+ end
278
+ end
279
+
280
+ class ShowSettings < AbstractSettings
281
+ attr_reader :model_class_name_block
282
+
283
+ def column_config_args
284
+ {}
285
+ end
286
+
287
+ def model_class_name(&block)
288
+ @model_class_name_block = block
289
+ end
103
290
  end
104
291
  end