Class: ActiveScaffold::Config::Core

Inherits:
Base show all
Includes:
OrmChecks
Defined in:
lib/active_scaffold/config/core.rb

Overview

to fix the ckeditor bridge problem inherit from full class name

Defined Under Namespace

Classes: UserColumns, UserSettings

Constant Summary collapse

@@plugin_directory =
File.expand_path(__FILE__).match(%{(^.*)/lib/active_scaffold/config/core.rb})[1]
@@frontend =
:default
@@theme =
:default
true
@@cache_association_options =
true
@@conditional_get_support =
false
@@store_user_settings =
true
@@dhtml_history =
true
ActiveScaffold::DataStructures::ActionLinks.new
@@ignore_columns =
ActiveScaffold::DataStructures::Set.new
true
@@timestamped_messages =
false
@@highlight_messages =
nil
@@after_config_callbacks =
[:_configure_sti]

Constants inherited from Base

Base::NO_FORMATS

Instance Attribute Summary collapse

Attributes inherited from Base

#action_group, #core, #formats, #user_settings_key

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OrmChecks

active_record?, #cast, cast, column_type, columns, columns_hash, content_columns, default_value, mongoid?, quoted_table_name, reflect_on_all_associations, table_name, tableless?, type_for_attribute

Methods inherited from Base

#crud_type, inherited, #new_user_settings, #setup_user_setting_key, #user

Methods included from ActiveScaffold::Configurable

#configure

Constructor Details

#initialize(model_id) ⇒ Core

internal usage only below this point




164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/active_scaffold/config/core.rb', line 164

def initialize(model_id)
  # model_id is the only absolutely required configuration value. it is also not publicly accessible.
  @model_id = model_id
  setup_user_setting_key

  # inherit the actions list directly from the global level
  @actions = self.class.actions.clone

  # create a new default columns datastructure, since it doesn't make sense before now
  attribute_names = _columns.collect { |c| c.name.to_sym }.sort_by(&:to_s)
  association_column_names = _reflect_on_all_associations.collect { |a| a.name.to_sym }
  if defined?(ActiveMongoid) && model < ActiveMongoid::Associations
    association_column_names.concat model.am_relations.keys.map(&:to_sym)
  end
  @columns = ActiveScaffold::DataStructures::Columns.new(model, attribute_names + association_column_names.sort_by(&:to_s))

  # and then, let's remove some columns from the inheritable set.
  content_columns = Set.new(_content_columns.map(&:name))
  @columns.exclude(*self.class.ignore_columns)
  @columns.exclude(*@columns.find_all { |c| c.column && content_columns.exclude?(c.column.name) }.collect(&:name))
  @columns.exclude(*model.reflect_on_all_associations.collect { |a| a.foreign_type.to_sym if a.options[:polymorphic] }.compact)

  # inherit the global frontend
  @frontend = self.class.frontend
  @theme = self.class.theme
  @cache_action_link_urls = self.class.cache_action_link_urls
  @cache_association_options = self.class.cache_association_options
  @conditional_get_support = self.class.conditional_get_support
  @store_user_settings = self.class.
  @sti_create_links = self.class.sti_create_links

  # inherit from the global set of action links
  @action_links = self.class.action_links.clone
  @timestamped_messages = self.class.timestamped_messages
  @highlight_messages = self.class.highlight_messages
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

configuration routing. we want to route calls named like an activated action to that action’s global or local Config class.




236
237
238
# File 'lib/active_scaffold/config/core.rb', line 236

def method_missing(name, *args)
  self[name] || super
end

Instance Attribute Details

action links are used by actions to tie together. they appear as links for each record, or general links for the ActiveScaffold.



143
144
145
# File 'lib/active_scaffold/config/core.rb', line 143

def action_links
  @action_links
end

#actionsObject

provides read/write access to the local Actions DataStructure



105
106
107
# File 'lib/active_scaffold/config/core.rb', line 105

def actions
  @actions
end

enable caching of action link urls



125
126
127
# File 'lib/active_scaffold/config/core.rb', line 125

def cache_action_link_urls
  @cache_action_link_urls
end

#cache_association_optionsObject

enable caching of association options



128
129
130
# File 'lib/active_scaffold/config/core.rb', line 128

def cache_association_options
  @cache_association_options
end

#columnsObject

provides read/write access to the local Columns DataStructure



111
112
113
# File 'lib/active_scaffold/config/core.rb', line 111

def columns
  @columns
end

#conditional_get_supportObject

enable setting ETag and LastModified on responses and using fresh_when/stale? to respond with 304 and avoid rendering views



131
132
133
# File 'lib/active_scaffold/config/core.rb', line 131

def conditional_get_support
  @conditional_get_support
end

#frontendObject

lets you override the global ActiveScaffold frontend for a specific controller



119
120
121
# File 'lib/active_scaffold/config/core.rb', line 119

def frontend
  @frontend
end

#highlight_messagesObject

a hash of string (or array of strings) and highlighter string to highlight words in messages. It will use highlight rails helper



158
159
160
# File 'lib/active_scaffold/config/core.rb', line 158

def highlight_messages
  @highlight_messages
end

#label(options = {}) ⇒ Object



147
148
149
# File 'lib/active_scaffold/config/core.rb', line 147

def label(options = {})
  as_(@label, options) || model.model_name.human(options.merge(options[:count].to_i == 1 ? {} : {:default => model.name.pluralize}))
end

#model_idObject (readonly)

some utility methods




280
281
282
# File 'lib/active_scaffold/config/core.rb', line 280

def model_id
  @model_id
end

#sti_childrenObject

STI children models, use an array of model names



152
153
154
# File 'lib/active_scaffold/config/core.rb', line 152

def sti_children
  @sti_children
end

lets you specify whether add a create link for each sti child for a specific controller



137
138
139
# File 'lib/active_scaffold/config/core.rb', line 137

def sti_create_links
  @sti_create_links
end

#store_user_settingsObject

enable saving user settings in session (per_page, limit, page, sort, search params)



134
135
136
# File 'lib/active_scaffold/config/core.rb', line 134

def 
  @store_user_settings
end

#themeObject

lets you override the global ActiveScaffold theme for a specific controller



122
123
124
# File 'lib/active_scaffold/config/core.rb', line 122

def theme
  @theme
end

#timestamped_messagesObject

prefix messages with current timestamp, set the format to display (you can use I18n keys) or true and :short will be used



155
156
157
# File 'lib/active_scaffold/config/core.rb', line 155

def timestamped_messages
  @timestamped_messages
end

Class Method Details

.actions=(val) ⇒ Object



10
11
12
# File 'lib/active_scaffold/config/core.rb', line 10

def self.actions=(val)
  @@actions = ActiveScaffold::DataStructures::Actions.new(*val)
end

.asset_path(filename, frontend = self.frontend) ⇒ Object

must be a class method so the layout doesn’t depend on a controller that uses active_scaffold note that this is unaffected by per-controller frontend configuration.



309
310
311
# File 'lib/active_scaffold/config/core.rb', line 309

def self.asset_path(filename, frontend = self.frontend)
  "active_scaffold/#{frontend}/#{filename}"
end

.available_frontendsObject



320
321
322
323
# File 'lib/active_scaffold/config/core.rb', line 320

def self.available_frontends
  frontends_dir = Rails.root.join('vendor', 'plugins', ActiveScaffold::Config::Core.plugin_directory, 'frontends')
  Dir.entries(frontends_dir).reject { |e| e.match(/^\./) } # Get rid of files that start with .
end

.columnObject

access to default column configuration.



64
65
66
# File 'lib/active_scaffold/config/core.rb', line 64

def self.column
  ActiveScaffold::DataStructures::Column
end

.config_class(name) ⇒ Object



266
267
268
# File 'lib/active_scaffold/config/core.rb', line 266

def self.config_class(name)
  "ActiveScaffold::Config::#{name.to_s.camelcase}".constantize if config_class?(name)
end

.config_class?(name) ⇒ Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/active_scaffold/config/core.rb', line 270

def self.config_class?(name)
  ActiveScaffold::Config.const_defined? name.to_s.camelcase
end

.dhtml_history?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/active_scaffold/config/core.rb', line 46

def self.dhtml_history?
  @@dhtml_history ? true : false
end

.freezeObject



95
96
97
98
99
# File 'lib/active_scaffold/config/core.rb', line 95

def self.freeze
  super
  security.freeze
  column.freeze
end

.ignore_columnsObject

columns that should be ignored for every model. these should be metadata columns like change dates, versions, etc. values in this array may be symbols or strings.



70
71
72
# File 'lib/active_scaffold/config/core.rb', line 70

def self.ignore_columns
  @@ignore_columns
end

.ignore_columns=(val) ⇒ Object



74
75
76
# File 'lib/active_scaffold/config/core.rb', line 74

def self.ignore_columns=(val)
  @@ignore_columns = ActiveScaffold::DataStructures::Set.new(*val)
end

.javascripts(frontend = self.frontend) ⇒ Object

must be a class method so the layout doesn’t depend on a controller that uses active_scaffold note that this is unaffected by per-controller frontend configuration.



315
316
317
318
# File 'lib/active_scaffold/config/core.rb', line 315

def self.javascripts(frontend = self.frontend)
  javascript_dir = File.join(Rails.public_path, 'javascripts', asset_path('', frontend))
  Dir.entries(javascript_dir).reject { |e| !e.match(/\.js$/) || (!dhtml_history? && e.match('dhtml_history')) }
end

.method_missing(name, *args) ⇒ Object



262
263
264
# File 'lib/active_scaffold/config/core.rb', line 262

def self.method_missing(name, *args)
  config_class(name) || super
end

.respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/active_scaffold/config/core.rb', line 274

def self.respond_to_missing?(name, include_all = false)
  config_class?(name) && @@actions.include?(name.to_s.underscore) || super
end

.securityObject

access to the permissions configuration. configuration options include:

* current_user_method - what method on the controller returns the current user. default: :current_user
* default_permission - what the default permission is. default: true


59
60
61
# File 'lib/active_scaffold/config/core.rb', line 59

def self.security
  ActiveScaffold::ActiveRecordPermissions
end

Instance Method Details

#[](action_name) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/active_scaffold/config/core.rb', line 244

def [](action_name)
  klass = self.class.config_class(action_name)
  return unless klass

  underscored_name = action_name.to_s.underscore.to_sym
  unless @actions.include? underscored_name
    raise "#{action_name.to_s.camelcase} is not enabled. Please enable it or remove any references in your configuration (e.g. config.#{underscored_name}.columns = [...])."
  end
  @action_configs ||= {}
  @action_configs[underscored_name] ||= klass.new(self)
end

#[]=(action_name, action_config) ⇒ Object



256
257
258
259
# File 'lib/active_scaffold/config/core.rb', line 256

def []=(action_name, action_config)
  @action_configs ||= {}
  @action_configs[action_name] = action_config
end

#_cache_lazy_valuesObject

To be called before freezing



202
203
204
205
206
207
208
209
210
# File 'lib/active_scaffold/config/core.rb', line 202

def _cache_lazy_values
  action_links.each(&:name_to_cache) if cache_action_link_urls
  columns.select(&:sortable?).each(&:sort)
  columns.select(&:searchable?).each(&:search_sql)
  actions.each do |action_name|
    action = send(action_name)
    Array(action.class.columns_collections).each { |method| action.send(method) }
  end
end

#_configure_stiObject

To be called after your finished configuration



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/active_scaffold/config/core.rb', line 213

def _configure_sti
  return if sti_children.nil?
  column = model.inheritance_column
  if sti_create_links
    columns[column].form_ui ||= :hidden
  else
    columns[column].form_ui ||= :select
    columns[column].options ||= {}
    columns[column].options[:options] = sti_children.collect do |model_name|
      [model_name.to_s.camelize.constantize.model_name.human, model_name.to_s.camelize]
    end
  end
end

#_setup_action(action) ⇒ Object



227
228
229
230
231
# File 'lib/active_scaffold/config/core.rb', line 227

def _setup_action(action)
  define_singleton_method action do
    self[action]
  end
end

#add_sti_create_links?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/active_scaffold/config/core.rb', line 138

def add_sti_create_links?
  sti_create_links && !sti_children.nil?
end

#build_action_columns(action, columns) ⇒ Object



296
297
298
299
300
301
302
303
304
305
# File 'lib/active_scaffold/config/core.rb', line 296

def build_action_columns(action, columns)
  action_columns =
    if columns.is_a?(ActiveScaffold::DataStructures::ActionColumns)
      columns.dup
    else
      ActiveScaffold::DataStructures::ActionColumns.new(*columns)
    end
  action_columns.action = action.is_a?(Symbol) ? send(action) : action
  action_columns
end

#inherited_view_pathsObject

warning - this won’t work as a per-request dynamic attribute in rails 2.0. You’ll need to interact with Controller#generic_view_paths



292
293
294
# File 'lib/active_scaffold/config/core.rb', line 292

def inherited_view_paths
  @inherited_view_paths ||= []
end

#modelObject Also known as: active_record_class



282
283
284
# File 'lib/active_scaffold/config/core.rb', line 282

def model
  @model ||= @model_id.to_s.camelize.constantize
end

#primary_keyObject



287
288
289
# File 'lib/active_scaffold/config/core.rb', line 287

def primary_key
  mongoid? ? '_id' : model.primary_key
end

#respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/active_scaffold/config/core.rb', line 240

def respond_to_missing?(name, include_all = false)
  self.class.config_class?(name) && @actions.include?(name.to_sym) || super
end