activesupport

What is mattr_accessor in a Rails module?

I couldn't really find this in Rails documentation but it seems like 'mattr_accessor' is the Module corollary for 'attr_accessor' (getter & setter) in a normal Ruby class. Eg. in a class class User attr_accessor :name def set_fullname @name = "#{self.first_name} #{self.last_name}" end end Eg. in a module module Authentica...

Rails ActiveSupport Time Parsing?

Rails' ActiveSupport module extends the builtin ruby Time class with a number of methods. Notably, there is the to_formatted_s method, which lets you write Time.now.to_formatted_s(:db) to get a string in Database format, rather than having to write ugly strftime format-strings everywhere. My question is, is there a way to go backwards?...

How do you deal with the conflict between ActiveSupport::JSON and the JSON gem?

I am stumped with this problem. ActiveSupport::JSON defines to_json on various core objects and so does the JSON gem. However, the implementation is not the same -- the ActiveSupport version takes arguments and the JSON gem version doesn't. I installed a gem that required the JSON gem and my app broke. The issue is that I'm using to_js...

Is there a setup_class/teardown_class for Rails tests?

I need to have a setup and teardown method for some Rails tests that is class or system wide, yet I have only found a way to define a regular setup/teardown that works on a per test level. For example: class ActiveSupport::TestCase setup do puts "Setting up" end teardown do puts "tearing down" end end will execute th...

Why are autoload, load_all! and require all used in active_support.rb?

I was looking at active_support.rb to try to understand the load process it uses. It uses three loading methods: load_all!, autoload and require. Why use three different ways of loading in the same file? module ActiveSupport def self.load_all! [Dependencies, Deprecation, Gzip, MessageVerifier, Multibyte, SecureRandom, TimeWithZon...

How do I work with Time in Rails?

I've been pulling my hair out trying to work with Time in Rails. Basically I need to set all time output (core as well as ActiveSupport) to the server's local time -- no GMT, no UTC, etc. I've seen various posts relating to Time, but they usually involve someone's need to set it for each user. Mine isn't nearly as complex, I simply wa...

can't activate activesupport (>= 2.3.2, runtime), already activated activesupport-2.1.2. what does it mean?

Hi, while trying to start some old revision of an opensource rails project confronted with a cloudy error message: "can't activate activesupport (>= 2.3.2, runtime), already activated activesupport-2.1.2" What does it mean? Either versions of rails and activesupport are installed on my box. I'm confused... ...

How do you know when to use an XML parser and when to use ActiveResource?

I tried using ActiveResource to parse a web service that was more like a HTML document and I kept getting a 404 error. Do I need to use an XML parser for this task instead of ActiveResource? My guess is that ActiveResource is only useful if you are consuming data from another Rails app and the XML data is easily translatable to a Rail...

Overloading ActiveSupport's default to_sentence behaviour

ActiveSupport offers the nice method to_sentence. Thus, require 'active_support' [1,2,3].to_sentence # gives "1, 2, and 3" [1,2,3].to_sentence(:last_word_connector => ' and ') # gives "1, 2 and 3" it's good that you can change the last word connector, because I prefer not to have the extra comma. but it takes so much extra text: 44 ...

ActiveRecord interfering with Logger

It appears that using ActiveRecord (which requires ActiveSupport) messes with the Logger class, resulting in difficulties. This can be seen with some example code: require 'rubygems' #require 'activerecord' require 'logger' log = Logger.new(STDERR) log.sev_threshold = Logger::INFO log.datetime_format = "%Y-%m-%d %H:%M:%S" log.debug "...

Comparison of String with ActiveSupport::Duration failed for time_ago_in_words

I get this error when I try to use time_ago_in_words: Comparison of String with ActiveSupport::Duration failed I'm trying to check whether an object was created more than 8 minutes ago: <% if time_ago_in_words(obj.created_at) > 8.minutes %> <p>Yes</p> <% end %> Would appreciate it if anyone knows the correct way to perform...

Rails 2.3.5: ActiveSupport CoreExtensions install trouble

I get the following error "uninitialized constant ActiveSupport::CoreExtensions" when trying to ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!( ?> :my_date => lambda { |time| time.strftime("%a, %b #{time.day.ordinalize}") All I'm trying to do is format dates in rails, but the bloody thing can't find Activesuppo...

Is ActiveSupport::Cache::MemoryStore cleared up rails restart with Passenger?

I am pretty sure it does, but I wanted to double check, since I don't know exactly how Passenger restarts a rails app. So if I have something like: Rails.cache.fetch(:my_obj) { MyObj.first } will all the cache be cleared upon restart (which is my hope)? ...

Why is class_inheritable_reader in ActiveSupport not documented?

Hi there, Pulling my hair out trying to figure out where class_inheritable_reader is documented in Rails. A Google search revealed its existence, and looking in the gem itself ya find the method: def class_inheritable_reader(*syms) syms.each do |sym| next if sym.is_a?(Hash) class_eval <<-EOS def self.#{sym} ...

Difference between mattr_accessor and cattr_accessor in ActiveSupport?

I can't work out from looking through the source what the difference is between the cattr_* and mattr_* methods provided in Class and Module respectively. I read this question: http://stackoverflow.com/questions/185573/what-is-mattr-accessor-in-a-rails-module which gives some details about both methods but doesn't highlight the differenc...

How can I dynamically determine which model a controller is controlling?

I'm writing a Rails plugin, and need to be able to dynamically determine which model a controller is associated with. For example, if I have a PeopleController, I need a clean way to determine that the controller handles Person models. I've had a look through the API and haven't found a method for it. Is the only way to do this as a re...

Rails.cache.fetch, Symbols, & Memcached

I have a rails 2.3.4 app and a line that looks like: temp = Rails.cache.fetch(:temp_id) { User.find_by_name('Temp').id } and everything worked fine, until I decided to switch the caching layer to memcached by adding the following to my environment.rb: config.cache_store = :mem_cache_store Now the line which used to work fine gives...

Debugging breaks on Rails's "break" in ActiveSupport::Callbacks.run

Ruby-debug is getting hung up by breaking on the "break" ruby reserved word on line 94 activesupport-2.3.5/lib/active_support/callbacks.rb. def run(object, options = {}, &terminator) enumerator = options[:enumerator] || :each unless block_given? send(enumerator) { |callback| callback.call(object) } else send(e...

converting Date object to TimeWithZone

I need to convert a Date object into a TimeWithZone object representing the beginning of that day in a given time zone. The following approach works, but seems too convoluted as it requires me to convert the date to a string: ?> date = Date.parse("2010-02-17") => Wed, 17 Feb 2010 >> ActiveSupport::TimeZone['Eastern Time (US & Canada)']...

Formatting Dates in Rails 3.0

I'm trying to format a date in Rails 3 using the new syntax as described in the code: http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/conversions.rb by using the following syntax in an initialiser: Date::DATE_FORMATS[:my_format] = '%m %d %Y' I am then referencing dates in my view like so: co...