Spina CMS
Spina Academy
Home Guides

Overriding Spina

Sometimes, Spina doesn't fit your needs exactly. Luckily it's very easy to override classes and methods when you really need to. We recommend using the approach mentioned in the Rails Guides which is outlined below.

Prepare your application

Start by adding the following code to application.rb:
```ruby
# config/application.rb
module MyApp
  class Application < Rails::Application
    # ...
    overrides = "#{Rails.root}/app/overrides"
    Rails.autoloaders.main.ignore(overrides)
    config.to_prepare do
      Dir.glob("#{overrides}/**/*_override.rb").each do |override|
        load override
      end
    end
  end
end

Overriding with class_eval

Now you can add files to your app/overrides directory. The simplest way to change a class is to open it using class_eval like this:
```ruby
# app/overrides/controllers/spina/pages_controller_override.rb
module Spina
  PagesController.class_eval do

    def homepage
      # Let's make it shoot out fireworks instead 
      render plain: 🎆
    end

  end
end

Overriding with prepend

Using class_eval a lot has some drawbacks, so another way to change Spina's classes is to prepend a module to existing classes. This gives you more control over the way you override functionality.
```ruby
# app/overrides/controllers/spina/pages_controller_override.rb
module Spina
  module PagesControllerOverride
    extend ActiveSupport::Concern
    
    prepended do
      before_action :do_something
    end

    # You can override methods and make them do something else...
    def homepage
      super
    end
    
    # ...or you can add new code
    def do_something
      # do something interesting
    end

  end
end
 
Spina::PagesController.prepend(Spina::PagesControllerOverride)