Is it possible to configure PaperTrail gem to ignore attributes globally? - paper-trail-gem

The PaperTrail gem docs state that you can configure individual models to ignore some attributes -- This works great, but I want to skip all updated_at attributes (in every model). Is there a way to do this globally (in an initializer?). Something like PaperTrail.config.ignore = [:updated_at]
Related question: Is there a list of global configuration options for the PaperTrail gem?

Currently (January 2017) there is no global model configuration in PaperTrail. You could do this with a global constant.
# config/initializers/global_constants.rb
GLOBAL_PT_IGNORE = [:updated_at]
# app/models/foo.rb
has_paper_trail(ignore: GLOBAL_PT_IGNORE + [:banana])
# app/models/bar.rb
has_paper_trail(ignore: GLOBAL_PT_IGNORE + [:kiwi, :mango])

Related

Run annotate gem on presenters

The README for the annotate gem mentions models, fixtures, specs, and files generated by specific gems, but not presenters.
Is there any way to make annotate add annotations to presenters?
There doesn't seem to be a gem-supported way to add model annotations to other types of classes which wrap around models, like presenters, decorators, and exhibitors. But, since pretty much all the main code in the gem is in a single file (lib/annotate/annotate_models.rb), you could bundle open annotate and hack around on it to get what you want, assuming you don't mind a bit of monkey patching. For example, assuming your presenters are in app/presenters, you could make the following kinds of edits to the file directly:
module AnnotateModels
# Towards the top of the file where all the directory
# declarations are...
PRESENTER_DIR = File.join("app", "presenters")
PRESENTER_TEST_DIR = File.join("test", "presenters")
PRESENTER_SPEC_DIR = File.join("spec", "presenters")
PRESENTER_PATTERNS = [
File.join(PRESENTER_DIR, "%MODEL_NAME%_presenter.rb"),
File.join(PRESENTER_TEST_DIR, "%MODEL_NAME%_presenter_test.rb"),
File.join(PRESENTER_SPEC_DIR, "%MODEL_NAME%_presenter_spec.rb"),
]
# ...
def annotate
# ...
# add in presenters to the list of file types that require annotations
%w(test fixture factory serializer presenter).each do |key|
# ...
end
end
def remove_annotations
# ...
# add presenter files to list needing annotation removal
(TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS + SERIALIZER_PATTERNS + PRESENTER_PATTERNS).map { ... }
end
# ...
end
Certainly not an elegant solution, but if this kind of change works for you, you could consider forking the gem and moving any changes there, or maybe even submit a pull request back to the gem. Looking at the annotate gem issue tracker, there doesn't seem to have been any feature requests or discussion around the addition of presenters or decorators to the file types that could be annotated.

Convention For Gem with Common Name

I recently authored a gem called 'setting' (found here). The extends ActiveRecord with a module named 'Setting'. I understand that gems are supposed to use the namespace they are named, however when testing this caused collisions with ActiveRecord models with the same name (a Setting model). Does a standard exist for creating a private module namespace? I don't need users of the gem to ever access the module outside the extension in ActiveRecord. Do I have any options outside of picking a less common name?
Since you're writing an Active Record extension, you could place your module inside the ActiveRecord namespace:
module ActiveRecord
module Setting
end
end
Other than that, no, there's no practical namespace solution for gems with very common names.

Problem switching from DataMapper to Active Record in Rails 3

I have removed my DataMapper specific models and gems in my Rakefile and removed all databases. I also updated my database.yml file. Now, when I attempt to generate a model with
rails g model Car year:integer make:string model:string
I get:
No value provided for required options '--orm'
Is there someplace that I am missing the specification of Active Record? I've been unable to find any documentation for switching an application's ORM.
Have a look in config/application.rb, you may have a line that looks like this:
config.generators do |g|
g.orm :datamapper
end
Change that :datamapper symbol to :active_record or remove that line completely to switch back to ActiveRecord.
If it's not there, you may have a file in config/initializers which does this setup for you.

Backward compatibility issues with json libraries in rails 2.3.4

to_json method in rails 2.0.2 for objects(say user model object with id and name)used to give simple output in the form
{"id":"xyz","name":"hello"}
But while upgrading the rails to 2.3.4 it gives the output as
{"user":{"id":"xyz","name":"hello"}}
This is breaking my most of the frontend code.
Is there way to get the results in the same fashion as in 2.0.2 ???
Looking for a speedy answer from you guys!!
You need to edit the file config/initializers/new_rails_defaults.rb and change ActiveRecord::Base.include_root_in_json = true to false. That should do it. See the to_json documentation.
If for some reason you don't have that file, this is what it should look like in a default Rails 2.3.x app.
# Be sure to restart your server when you modify this file.
# These settings change the behavior of Rails 2 apps and will be defaults
# for Rails 3. You can remove this initializer when Rails 3 is released.
if defined?(ActiveRecord)
# Include Active Record class name as root for JSON serialized output.
ActiveRecord::Base.include_root_in_json = true
# Store the full class name (including module namespace) in STI type column.
ActiveRecord::Base.store_full_sti_class = true
end
# Use ISO 8601 format for JSON serialized times and dates.
ActiveSupport.use_standard_json_time_format = true
# Don't escape HTML entities in JSON, leave that for the #json_escape helper.
# if you're including raw json in an HTML page.
ActiveSupport.escape_html_entities_in_json = false

Using Warbler, how do I exclude Active Record from the bundled gems?

When using Warbler, what line(s) do I need to add to config/warble.rb to keep it from including Active Record in the bundled gems. I already have excluded Active Record in config/environment.rb as shown below.
config.frameworks -= [ :active_record ]
I tried the same thing only using config.gems in config/warble.rb, but to no avail.
I haven't been able to try either of these ideas, but looking at Nick Sieger's examples :-
Does the gem name have to be a String rather than a Symbol?
It looks like activerecord maybe being included implicitly because config.gems includes rails and config.gem_dependencies = true. Maybe you need to change config.gem_dependencies to false and explicitly include rails, actioncontroller, etc in config.gems.
It might be instructive to print out or log the value of config.gems from within the warble.rb file.

Resources