Class: AdminAssistant

Inherits:
Object
  • Object
show all
Defined in:
lib/admin_assistant.rb,
lib/admin_assistant/index.rb,
lib/admin_assistant/model.rb,
lib/admin_assistant/route.rb,
lib/admin_assistant/column.rb,
lib/admin_assistant/helper.rb,
lib/admin_assistant/search.rb,
lib/admin_assistant/builder.rb,
lib/admin_assistant/form_view.rb,
lib/admin_assistant/show_view.rb,
lib/admin_assistant/request/new.rb,
lib/admin_assistant/request/base.rb,
lib/admin_assistant/request/edit.rb,
lib/admin_assistant/request/show.rb,
lib/admin_assistant/request/index.rb,
lib/admin_assistant/request/create.rb,
lib/admin_assistant/request/update.rb,
lib/admin_assistant/virtual_column.rb,
lib/admin_assistant/has_many_column.rb,
lib/admin_assistant/request/destroy.rb,
lib/admin_assistant/paperclip_column.rb,
lib/admin_assistant/belongs_to_column.rb,
lib/admin_assistant/association_target.rb,
lib/admin_assistant/active_record_column.rb,
lib/admin_assistant/request/autocomplete.rb,
lib/admin_assistant/default_search_column.rb,
lib/admin_assistant/polymorphic_belongs_to_column.rb

Defined Under Namespace

Modules: ControllerClassMethods, ControllerMethods, FormViewMethods, Helper, Request Classes: AbstractSettings, ActiveRecordColumn, AssociationTarget, BaseSettings, BelongsToColumn, Builder, Column, ColumnConfig, DefaultSearchColumn, Engine, FormSettings, FormView, HasManyColumn, Index, IndexSettings, Model, MultiFormView, PaperclipColumn, PolymorphicBelongsToColumn, Route, Search, ShowSettings, ShowView, VirtualColumn

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller_class, model_class) ⇒ AdminAssistant

Returns a new instance of AdminAssistant.



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

def initialize(controller_class, model_class)
  @controller_class, @model_class = controller_class, model_class
  @model = Model.new model_class
  @actions = [:index, :create, :update, :show]
  @form_settings = FormSettings.new self
  @index_settings = IndexSettings.new self
  @show_settings = ShowSettings.new self
  @base_settings = BaseSettings.new self
  @added_default_search_matches = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/admin_assistant.rb', line 169

def method_missing(meth, *args)
  if crudful_request_methods.include?(meth) and args.size == 1
    self.class.request_start_time = Time.now if ENV['PROFILE_LOGGING']
    Request.dispatch meth, self, args.first
  elsif autocomplete_actions && autocomplete_actions.include?(meth)
    Request.dispatch :autocomplete, self, args.first
  elsif meth.to_s =~ /(.*)\?/ && crudful_request_methods.include?($1.to_sym)
    supports_action? $1
  else
    super
  end
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



51
52
53
# File 'lib/admin_assistant.rb', line 51

def actions
  @actions
end

#added_default_search_matchesObject (readonly)

Returns the value of attribute added_default_search_matches.



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

def added_default_search_matches
  @added_default_search_matches
end

#base_settingsObject (readonly)

Returns the value of attribute base_settings.



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

def base_settings
  @base_settings
end

#controller_classObject (readonly)

Returns the value of attribute controller_class.



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

def controller_class
  @controller_class
end

#custom_destroyObject

Returns the value of attribute custom_destroy.



51
52
53
# File 'lib/admin_assistant.rb', line 51

def custom_destroy
  @custom_destroy
end

#default_search_matches_onObject



160
161
162
163
164
165
166
167
# File 'lib/admin_assistant.rb', line 160

def default_search_matches_on
  if @default_search_matches_on
    @default_search_matches_on
  else
    @model.searchable_columns.map(&:name).
           concat(added_default_search_matches)
  end
end

#form_settingsObject (readonly)

Returns the value of attribute form_settings.



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

def form_settings
  @form_settings
end

#index_settingsObject (readonly)

Returns the value of attribute index_settings.



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

def index_settings
  @index_settings
end

#model_classObject (readonly)

Returns the value of attribute model_class.



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

def model_class
  @model_class
end

#model_class_nameObject



182
183
184
185
# File 'lib/admin_assistant.rb', line 182

def model_class_name
  @model_class_name ||
      @model_class.name.gsub(/([A-Z])/, ' \1')[1..-1].downcase
end

#show_settingsObject (readonly)

Returns the value of attribute show_settings.



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

def show_settings
  @show_settings
end

Class Method Details

.all_files_under(*paths) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/admin_assistant.rb', line 23

def self.all_files_under(*paths)
  paths.flatten!
  paths.map! { |p| Pathname.new(p) }
  files = paths.select { |p| p.file? }
  (paths - files).each do |dir|
    files << all_files_under(dir.children)
  end
  files.flatten.map(&:to_s)
end

.app_uses_sprockets?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/admin_assistant.rb', line 33

def self.app_uses_sprockets?
  # For now, we assume you're using Sprockets if app/assets exists.
  File.exist?("#{Rails.root}/app/assets")
end

.profile(msg) ⇒ Object



38
39
40
41
42
# File 'lib/admin_assistant.rb', line 38

def self.profile(msg)
  if self.request_start_time
    Rails.logger.info "#{msg}: #{Time.now - self.request_start_time}"
  end
end

.template_file(template_name) ⇒ Object



44
45
46
# File 'lib/admin_assistant.rb', line 44

def self.template_file(template_name)
  "#{File.dirname(__FILE__)}/views/#{template_name}.html.erb"
end

Instance Method Details

#[](name) ⇒ Object



65
66
67
# File 'lib/admin_assistant.rb', line 65

def [](name)
  @base_settings[name]
end

#accumulate_belongs_to_columns(names) ⇒ Object



82
83
84
# File 'lib/admin_assistant.rb', line 82

def accumulate_belongs_to_columns(names)
  accumulate_columns(names).select { |column| column.is_a?(BelongsToColumn) }
end

#accumulate_columns(names) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/admin_assistant.rb', line 69

def accumulate_columns(names)
  columns = @model.paperclip_attachments.map { |paperclip_attachment|
    PaperclipColumn.new paperclip_attachment
  }
  names.each do |column_name|
    unless columns.any? { |column| column.contains?(column_name) }
      column = column column_name
      columns << column if column
    end
  end
  columns
end

#autocomplete_actionsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/admin_assistant.rb', line 86

def autocomplete_actions
  @autocomplete_actions ||= begin
    ac_actions = []
    if [:new, :create, :edit, :update].any? { |action|
      actions.include?(action)
    }
      @model.belongs_to_associations.each do |assoc|
        ac_actions << "autocomplete_#{assoc.name}".to_sym
      end
    end
    if actions.include?(:index)
      base_settings.all_polymorphic_types.each do |p_type|
        ac_actions << "autocomplete_#{p_type.name.underscore.downcase}".to_sym
      end
    end
    ac_actions.uniq
  end
  @autocomplete_actions
end

#column(name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/admin_assistant.rb', line 106

def column(name)
  if @model.paperclip_attachments.include?(name)
    PaperclipColumn.new name
  elsif (belongs_to_assoc = @model.belongs_to_assoc(name) or
         belongs_to_assoc = @model.belongs_to_assoc_by_foreign_key(name))
    column_based_on_belongs_to_assoc name, belongs_to_assoc
  elsif belongs_to_assoc = @model.belongs_to_assoc_by_polymorphic_type(name)
    # skip it, actually
  elsif (ar_column = @model_class.columns_hash[name.to_s])
    ActiveRecordColumn.new ar_column
  elsif has_many_assoc = @model.has_many_assoc(name)
    HasManyColumn.new(
      has_many_assoc,
      :match_text_fields_in_search => 
          search_settings[name].match_text_fields_for_association?
    )
  else
    VirtualColumn.new name, @model_class, self
  end
end

#column_based_on_belongs_to_assoc(name, belongs_to_assoc) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/admin_assistant.rb', line 127

def column_based_on_belongs_to_assoc(name, belongs_to_assoc)
  if belongs_to_assoc.options[:polymorphic]
   PolymorphicBelongsToColumn.new belongs_to_assoc
  else
    BelongsToColumn.new(
      belongs_to_assoc,
      :match_text_fields_in_search => 
          search_settings[name].match_text_fields_for_association?,
      :sort_by => index_settings[name].sort_by
    )
  end
end

#controller_actionsObject



140
141
142
143
144
145
146
# File 'lib/admin_assistant.rb', line 140

def controller_actions
  c_actions = actions.clone
  c_actions << :new if c_actions.include?(:create)
  c_actions << :edit if c_actions.include?(:update)
  c_actions.concat(autocomplete_actions) if autocomplete_actions
  c_actions
end

#controller_css_class(controller) ⇒ Object



148
149
150
# File 'lib/admin_assistant.rb', line 148

def controller_css_class(controller)
  controller.controller_path.gsub(%r|/|, '_')
end

#crudful_request_methodsObject



152
153
154
# File 'lib/admin_assistant.rb', line 152

def crudful_request_methods
  [:create, :destroy, :edit, :index, :new, :update, :show]
end

#default_column_namesObject



156
157
158
# File 'lib/admin_assistant.rb', line 156

def default_column_names
  @model.default_column_names
end

#paperclip_attachmentsObject



187
188
189
# File 'lib/admin_assistant.rb', line 187

def paperclip_attachments
  @model.paperclip_attachments
end

#search_settingsObject



191
192
193
# File 'lib/admin_assistant.rb', line 191

def search_settings
  @index_settings.search_settings
end

#supports_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
198
199
200
201
202
203
204
# File 'lib/admin_assistant.rb', line 195

def supports_action?(action)
  @memoized_action_booleans ||= {}
  unless @memoized_action_booleans.has_key?(action)
    @memoized_action_booleans[action] =
        @controller_class.public_instance_methods.any? { |meth|
          meth.to_sym == action.to_sym
        } 
  end
  @memoized_action_booleans[action]
end

#url_params(a = action) ⇒ Object



206
207
208
# File 'lib/admin_assistant.rb', line 206

def url_params(a = action)
  {:controller => @controller_class.controller_path, :action => a}
end