0

I am new to Ruby on rails. i am using rails 4.1.6 and what i want is to make log of all the process in one text file (like index page accessed, view page access etc..). for that i want to create common function, in which i can pass my text as agrs, i had do some R&D on that and i found this : In Rails, where to put useful functions for both controllers and models, but it seems that it is not working with active admin resources. so, for active admin controller and model, i have to create any other modules (i.e. on other location let say /admin/) or anything else i have to do ?

is there any global location that we can use in active admin like component in cakephp.

thanks

EDIT

app/admin/driver.rb

ActiveAdmin.register User, as: 'Driver' do
    ...
    ...
    index :download_links => false do
        ...
        ...
        #call function to maintain log something like,
        take_note('action performed')
    end
Community
  • 1
  • 1
Dr Magneto
  • 981
  • 1
  • 8
  • 18
  • Can you elaborate on what the code does that you want to share and why you need to share it. IMO it can make a huge difference if the code for examples does something view specific or data sanitizing. – spickermann Oct 22 '16 at 07:40

2 Answers2

0

Easiest way is to create a file in the config/initializers folder - these will be autoloaded.

You could also write it in application.rb, though I recommend only doing this for configuration.

A common pattern is to add lib/ to the autoload path so any custom files there can be used - see Auto-loading lib files in Rails 4

It's maybe worth mentioning that you can in fact access your models from anywhere as well.


for your comment

here's a generic class which you can write in lib/ if you add it to your autoload path

class MyClass
  def self.my_class_method
    puts "i was called"
  end
end

Then calling it from anywhere else ...

MyClass.my_class_method
Community
  • 1
  • 1
max pleaner
  • 26,189
  • 9
  • 66
  • 118
0

A global method feel like a code smell to me. Instead I would create a Note class or module. This doesn't pollute the global name space and is easier to test in isolation.

I would add code like this in a initializer:

# in config/initializers/note.rb
module Note
  def self.take(message)
    # log `message`
  end
end

It could be used in your controller like this:

index :download_links => false do
  # ...
  Note.take('action performed')
end

Please note that you need to restart your server when changing files in the config folder.

spickermann
  • 100,941
  • 9
  • 101
  • 131