Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
activesupport-decorators
Advanced tools
The decorator pattern is particularly useful when extending constants in rails engines or vice versa. To implement the decorator pattern, you need to load the decorator after the original file has been loaded. When you reference a class in a Rails application, ActiveSupport will only load the first file it finds that matches the class name. This means that you need to manually load the additional (decorator) file or eager load all of them on application startup. This is a tiny gem that provides you with a simple way to tell ActiveSupport to load your decorator files when needed.
Add it to your Gemfile and run bundle install:
gem 'activesupport-decorators', '~> 2.1'
Your engine defines a model called Pet (in my_engine/app/models/pet.rb):
class Pet < ActiveRecord::Base
end
Your application now wants to adds the concept of pet owners. You extend the Pet model in the application with the following model decorator (in app/models/pet_decorator.rb). Note that you could use 'Pet.class_eval do' instead of 'class Pet' if you want.
class Pet
belongs_to :owner
end
Now tell ActiveSupportDecorators where to look for decorators, similar to setting Rails autoload paths. This will load a decorator file if it matches the original file's name/path and ends with '_decorator.rb'. In other words when the engine's app/pet.rb is loaded, it will load the main applications app/pet_decorator.rb.
ActiveSupportDecorators.paths << File.join(Rails.application.root, 'app/**')
Note that '**' is added to the end of the path because this is the pattern that Rails autoloads your app folder with. It means that every folder inside app is regarded as a autoload path instead of app/ itself, so that you can call your model 'Pet' and not 'Models::Pet'.
Similar to the example above except the initializer is placed in the engine instead of the main application. The example below will cause any matching decorator file in my_engine/app to load when a file is loaded from app/.
module MyEngine
module Rails
class Engine < ::Rails::Engine
initializer :set_decorator_paths, :before => :load_environment_hook do |app|
ActiveSupportDecorators.paths << File.join(config.root, 'app/**')
end
end
end
end
ActiveSupportDecorators.debug = true
ActiveSupport::Dependencies.autoload_paths
ActiveSupportDecorators.expanded_paths
Other gems work by simply telling Rails to eager load all your decorators on application startup as seen [here] (https://github.com/atd/rails_engine_decorators/blob/master/lib/rails_engine_decorators/engine/configuration.rb) and here. They expect your decorators to use 'MyClass.class_eval do' to extend the original class as this is what triggers the original class to be loaded. Disadvantages of this approach include:
This gem works by hooking into ActiveSupport, which means that decorators are loaded as required instead of at application startup. You can use 'class MyClass' and expect that other classes are already decorated, since when you reference other classes they will be decorated on the fly when ActiveSupport loads them.
FAQs
Unknown package
We found that activesupport-decorators demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.