Module: ActiveScaffold::Core::ClassMethods

Defined in:
lib/active_scaffold/core.rb

Defined Under Namespace

Modules: Prefixes

Instance Method Summary collapse

Instance Method Details

To be called after include action modules



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/active_scaffold/core.rb', line 130

def _add_sti_create_links
  new_action_link = active_scaffold_config.action_links.collection['new']
  unless new_action_link.nil? || active_scaffold_config.sti_children.empty?
    active_scaffold_config.action_links.collection.delete('new')
    active_scaffold_config.sti_children.each do |child|
      new_sti_link = Marshal.load(Marshal.dump(new_action_link)) # deep clone
      new_sti_link.label = as_(:create_model, :model => child.to_s.camelize.constantize.model_name.human)
      new_sti_link.parameters = {:parent_sti => controller_path}
      new_sti_link.controller = Proc.new { active_scaffold_controller_for(child.to_s.camelize.constantize).controller_path }
      active_scaffold_config.action_links.collection.create.add(new_sti_link)
    end
  end
end

#active_scaffold(model_id = nil, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_scaffold/core.rb', line 71

def active_scaffold(model_id = nil, &block)
  extend Prefixes
  # initialize bridges here
  ActiveScaffold::Bridges.run_all

  # converts Foo::BarController to 'bar' and FooBarsController to 'foo_bar' and AddressController to 'address'
  model_id = self.to_s.split('::').last.sub(/Controller$/, '').pluralize.singularize.underscore unless model_id

  # run the configuration
  @active_scaffold_config = ActiveScaffold::Config::Core.new(model_id)
  @active_scaffold_config_block = block
  self.links_for_associations
  
  self.active_scaffold_superclasses_blocks.each {|superblock| self.active_scaffold_config.configure &superblock}
  self.active_scaffold_config.sti_children = nil # reset sti_children if set in parent block
  self.active_scaffold_config.configure &block if block_given?
  self.active_scaffold_config._configure_sti unless self.active_scaffold_config.sti_children.nil?
  self.active_scaffold_config._load_action_columns

  # defines the attribute read methods on the model, so record.send() doesn't find protected/private methods instead
  klass = self.active_scaffold_config.model
  # Rails 4.0.4 has removed attribute_methods_generated,
  # and made define_attribute_methods threadsave to call multiple times.
  # Check for that here.
  if ((Rails::VERSION::MAJOR == 4) && !klass.respond_to?(:attribute_methods_generated)) ||
      !klass.attribute_methods_generated?
    klass.define_attribute_methods
  end
  # include the rest of the code into the controller: the action core and the included actions
  module_eval do
    include ActiveScaffold::Finder
    include ActiveScaffold::Constraints
    include ActiveScaffold::AttributeParams
    include ActiveScaffold::Actions::Core
    active_scaffold_config.actions.each do |mod|
      name = mod.to_s.camelize
      include "ActiveScaffold::Actions::#{name}".constantize

      # sneak the action links from the actions into the main set
      mod_conf = active_scaffold_config.send(mod)
      if mod_conf.respond_to?(:link) && (link = mod_conf.link)
        if link.is_a? Array
          link.each {|current| active_scaffold_config.action_links.add_to_group(current, active_scaffold_config.send(mod).action_group)}
        elsif link.is_a? ActiveScaffold::DataStructures::ActionLink
          active_scaffold_config.action_links.add_to_group(link, active_scaffold_config.send(mod).action_group)
        end
      end
    end
  end
  self._add_sti_create_links if self.active_scaffold_config.add_sti_create_links?
end

#active_scaffold_configObject



207
208
209
210
211
212
213
# File 'lib/active_scaffold/core.rb', line 207

def active_scaffold_config
  if @active_scaffold_config.nil?
    self.superclass.active_scaffold_config if self.superclass.respond_to? :active_scaffold_config
  else
    @active_scaffold_config
  end
end

#active_scaffold_config_blockObject



215
216
217
# File 'lib/active_scaffold/core.rb', line 215

def active_scaffold_config_block
  @active_scaffold_config_block
end

#active_scaffold_config_for(klass) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/active_scaffold/core.rb', line 229

def active_scaffold_config_for(klass)
  begin
    controller = active_scaffold_controller_for(klass)
  rescue ActiveScaffold::ControllerNotFound
    config = ActiveScaffold::Config::Core.new(klass)
    config._load_action_columns
    config
  else
    controller.active_scaffold_config
  end
end

#active_scaffold_controller_for(klass) ⇒ Object



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

def active_scaffold_controller_for(klass)
  ActiveScaffold::Core.active_scaffold_controller_for(klass, self.to_s.split('::')[0...-1].join('::') + '::')
end

#active_scaffold_controller_for_column(column, options = {}) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/active_scaffold/core.rb', line 156

def active_scaffold_controller_for_column(column, options = {})
  begin
    if column.polymorphic_association?
      :polymorph
    elsif options.include?(:controller)
      "#{options[:controller].to_s.camelize}Controller".constantize
    else
      active_scaffold_controller_for(column.association.klass)
    end
  rescue ActiveScaffold::ControllerNotFound
    nil        
  end
end

#active_scaffold_superclasses_blocksObject



219
220
221
222
223
224
225
226
227
# File 'lib/active_scaffold/core.rb', line 219

def active_scaffold_superclasses_blocks
  blocks = []
  klass = self.superclass
  while klass.respond_to? :active_scaffold_superclasses_blocks
    blocks << klass.active_scaffold_config_block
    klass = klass.superclass
  end
  blocks.compact.reverse
end

#add_active_scaffold_path(path) ⇒ Object



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

def add_active_scaffold_path(path)
  as_path = File.join(ActiveScaffold::Config::Core.plugin_directory, 'app', 'views')
  index = view_paths.find_index { |p| p.to_s == as_path }
  if index
    self.view_paths = view_paths[0..index-1] + Array(path) + view_paths[index..-1]
  else
    append_view_path path
  end
end


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/active_scaffold/core.rb', line 170

def link_for_association(column, options = {})
  controller = active_scaffold_controller_for_column(column, options)
  
  unless controller.nil?
    options.reverse_merge! :position => :after, :type => :member, :controller => (controller == :polymorph ? controller : "/#{controller.controller_path}"), :column => column
    options[:parameters] ||= {}
    options[:parameters].reverse_merge! :association => column.association.name
    if column.plural_association?
      ActiveScaffold::DataStructures::ActionLink.new('index', options.merge(:refresh_on_close => true))
    else
      actions = controller.active_scaffold_config.actions unless controller == :polymorph
      actions ||= [:create, :update, :show] 
      column.actions_for_association_links.delete :new unless actions.include? :create
      column.actions_for_association_links.delete :edit unless actions.include? :update
      column.actions_for_association_links.delete :show unless actions.include? :show
      ActiveScaffold::DataStructures::ActionLink.new(nil, options.merge(:html_options => {:class => column.name}))
    end 
  end
end


190
191
192
193
194
195
# File 'lib/active_scaffold/core.rb', line 190

def link_for_association_as_scope(scope, options = {})
  options.reverse_merge! :label => scope, :position => :after, :type => :member, :controller => controller_path
  options[:parameters] ||= {}
  options[:parameters].reverse_merge! :named_scope => scope
  ActiveScaffold::DataStructures::ActionLink.new('index', options)
end

Create the automatic column links. Note that this has to happen when configuration is done, because otherwise the Nested module could be disabled. Actually, it could still be disabled later, couldn’t it?



145
146
147
148
149
150
151
152
153
154
# File 'lib/active_scaffold/core.rb', line 145

def links_for_associations
  return unless active_scaffold_config.actions.include? :list and active_scaffold_config.actions.include? :nested
  active_scaffold_config.columns.each do |column|
    next unless column.link.nil? and column.autolink?
    #lazy load of action_link, cause it was really slowing down app in dev mode
    #and might lead to trouble cause of cyclic constantization of controllers
    #and might be unnecessary cause it is done before columns are configured
    column.set_link(Proc.new {|col| link_for_association(col)})
  end
end

#uses_active_scaffold?Boolean

Returns:

  • (Boolean)


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

def uses_active_scaffold?
  !active_scaffold_config.nil?
end