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

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

Instance Attribute Summary collapse

Attributes inherited from Base

#action_group, #core, #formats, #user

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OrmChecks

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

Methods inherited from Base

#crud_type, inherited

Methods included from ActiveScaffold::Configurable

#configure

Constructor Details

#initialize(model_id) ⇒ Core

internal usage only below this point




149
150
151
152
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
181
182
183
# File 'lib/active_scaffold/config/core.rb', line 149

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

  # 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.




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

def method_missing(name, *args)
  @action_configs ||= {}
  titled_name = name.to_s.camelcase
  underscored_name = name.to_s.underscore.to_sym
  klass = "ActiveScaffold::Config::#{titled_name}".constantize rescue nil
  if klass
    if @actions.include? underscored_name
      return @action_configs[underscored_name] ||= klass.new(self)
    else
      raise "#{titled_name} is not enabled. Please enable it or remove any references in your configuration (e.g. config.#{underscored_name}.columns = [...])."
    end
  end
  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.



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

def action_links
  @action_links
end

#actionsObject

provides read/write access to the local Actions DataStructure



90
91
92
# File 'lib/active_scaffold/config/core.rb', line 90

def actions
  @actions
end

enable caching of action link urls



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

def cache_action_link_urls
  @cache_action_link_urls
end

#cache_association_optionsObject

enable caching of association options



113
114
115
# File 'lib/active_scaffold/config/core.rb', line 113

def cache_association_options
  @cache_association_options
end

#columnsObject

provides read/write access to the local Columns DataStructure



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

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



116
117
118
# File 'lib/active_scaffold/config/core.rb', line 116

def conditional_get_support
  @conditional_get_support
end

#frontendObject

lets you override the global ActiveScaffold frontend for a specific controller



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

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



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

def highlight_messages
  @highlight_messages
end

#label(options = {}) ⇒ Object



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

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




234
235
236
# File 'lib/active_scaffold/config/core.rb', line 234

def model_id
  @model_id
end

#sti_childrenObject

STI children models, use an array of model names



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

def sti_children
  @sti_children
end

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



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

def sti_create_links
  @sti_create_links
end

#store_user_settingsObject

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



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

def 
  @store_user_settings
end

#themeObject

lets you override the global ActiveScaffold theme for a specific controller



107
108
109
# File 'lib/active_scaffold/config/core.rb', line 107

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



140
141
142
# File 'lib/active_scaffold/config/core.rb', line 140

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.



252
253
254
# File 'lib/active_scaffold/config/core.rb', line 252

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

.available_frontendsObject



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

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

.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

.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.



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

def self.ignore_columns
  @@ignore_columns
end

.ignore_columns=(val) ⇒ Object



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

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.



258
259
260
261
# File 'lib/active_scaffold/config/core.rb', line 258

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



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

def self.method_missing(name, *args)
  klass = "ActiveScaffold::Config::#{name.to_s.camelcase}".constantize rescue nil
  return klass if @@actions.include?(name.to_s.underscore) && klass
  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

#_configure_stiObject

To be called after your finished configuration



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/active_scaffold/config/core.rb', line 195

def _configure_sti
  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

#_load_action_columnsObject

To be called after your finished configuration



186
187
188
189
190
191
192
# File 'lib/active_scaffold/config/core.rb', line 186

def _load_action_columns
  # then, register the column objects
  actions.each do |action_name|
    action = send(action_name)
    action.columns.set_columns(columns) if action.respond_to?(:columns)
  end
end

#add_sti_create_links?Boolean

Returns:

  • (Boolean)


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

def add_sti_create_links?
  sti_create_links && !sti_children.nil?
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



246
247
248
# File 'lib/active_scaffold/config/core.rb', line 246

def inherited_view_paths
  @inherited_view_paths ||= []
end

#modelObject Also known as: active_record_class



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

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

#primary_keyObject



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

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