Augmenting a model from an external gem
You will need to put your module_eval code into a config.to_prepare do
block. The easiest place to do this is in config/application.rb
or to create an engine. The code is identical except it executes every time you run the site not just the first time (which especially applies to development mode) and code which only executes before the initialisation process (aka requiring files) into a config.before_initialize do
block.
The reason that config.to_prepare
is important is because in development mode, the code is reloaded on every request but initializers generally aren't. This means that Page
, which you are running a module_eval on, will only have the module_eval run once but will reload itself every request. config.to_prepare
is a Rails hook that runs every single time providing great convenience for situations like this.
config/application.rb approach
class Application < Rails::Application
# ... other stuff ...
config.before_initialize do
require 'page_extensions'
end
config.to_prepare do
Page.send :include, Pants::Extensions::Page
end
end
Engine approach
If you don't want to modify config/application.rb
then you can, in Refinery CMS, create vendor/engines/add_page_extensions/lib/add_page_extensions.rb
which would look like this:
require 'refinery'
module Refinery
module AddPageExtensions
class Engine < Rails::Engine
config.before_initialize do
require 'page_extensions'
end
config.to_prepare do
Page.send :include, Pants::Extensions::Page
end
end
end
end
If you use the engines approach you will also need to create vendor/engines/add_page_extensions/add_page_extensions.gemspec
which should contain a simple gemspec:
Gem::Specification.new do |s|
s.name = 'add_page_extensions'
s.require_paths = %w(lib)
s.version = 1.0
s.files = Dir["lib/**/*"]
end
And then in your Gemfile
add this line:
gem 'add_page_extensions', :path => 'vendor/engines'
If you go the engine approach, you will probably want to put all of your logic inside the engine's lib
directory including the Pants::Extensions::Page
code.
Hope this helps
Is it possible to use gem faker from my yml file?
Out of the box, this is not possible with plain YAML. But when you run your YAML file through ERB before parsing then you can do that.
Change your YAML to
:users:
:ncpf: <%= Faker::Number.number %>
:birth: <%= Faker::Date.birthday %>
and read the file like this
require 'erb'
require 'json'
file = File.read('path/filename.yml')
yaml = ERB.new(file).result
DADOS = YAML.load(yaml)
Btw this is what Rails does internally with configuration files. So when you are using Rails then you can use this simplified version to load that file
DADOS = ActiveSupport::ConfigurationFile.parse('path/filename.yml')
Override a class in rails/refinerycms
Perhaps this would help you?
Augmenting a model from an external gem
parndt
Overriding ActionView::Base.field_error_proc in RefineryCMS
Turns out refineryCMS does indeed override field_error_proc:
https://github.com/refinery/refinerycms/issues/961
This worked for me:
# Uncomment the following block if you want each input field to have the validation messages attached.
Rails::Application.refinery.after_inclusion do
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
unless html_tag =~ /^<label/
%{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
else
%{<div class="field_with_errors">#{html_tag}</div>}.html_safe
end
end
end
Defining FactoryGirl for User model of Devise fails
OK, I finally found the problem. It turns out you have to restart "spork" when ever you make a change to the User model, because it preloads it.
Using Rails Migration on different database than standard production or development
A bit late, but I was dealing with this problem today and I came up with this custom rake task:
namespace :db do
desc "Apply db tasks in custom databases, for example rake db:alter[db:migrate,test-es] applies db:migrate on the database defined as test-es in databases.yml"
task :alter, [:task,:database] => [:environment] do |t, args|
require 'activerecord'
puts "Applying #{args.task} on #{args.database}"
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[args.database])
Rake::Task[args.task].invoke
end
end
Related Topics
How to Do Standard Deviation in Ruby
Ruby Net::Http::Get and JSON Responses
Best Way to Combine Fragment and Object Caching for Memcached and Rails
How to Render a Partial in Sinatra View (Haml in Haml)
Object.Valid? Returns False But Object.Errors.Full_Messages Is Empty
How to Use "Gets" on a Rake Task
How to Do Basic Authentication Over Https in Ruby
What Are the Things You Would Like Improved in the Ruby Language
Ruby: How to Chain Multiple Method Calls Together with "Send"
Which Ruby Memoize Pattern Does Activesupport::Memoizable Refer To
Ruby on Rails, Could Not Find a Valid Gem 'Rails'
Ruby on Rails, Two Models in One Form
Carrierwave Crop Specific Version
Deleting Table from Schema - Rails
How to Delete All Contents of a Folder with Ruby-Rails
Failed to Build JSON (1.6.3) with Native Extension After I Installed Xcode 4.2