activesupport

Can't set timezone using abbreviation

和自甴很熟 提交于 2019-12-04 05:58:40
问题 I can't set timezone on Rails using its abbreviation, for example: >> Time.zone = 'BRT' ArgumentError: Invalid Timezone: BRT from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active_support/core_ext/time/zones.rb:61:in `rescue in find_zone!' from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active_support/core_ext/time/zones.rb:53:in `find_zone!' from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems

Difference between mattr_accessor and cattr_accessor in ActiveSupport?

天涯浪子 提交于 2019-12-04 04:41:20
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: What is mattr_accessor in a Rails module? which gives some details about both methods but doesn't highlight the differences. So my question is what is the difference between them and why do we need both sets of methods when they are practically identical in the source? Also, which should we use when? Module is the superclass of the class Class so if a suitably generic name could be thought of then the methods for

How do I require ActiveSupport's rescue_from method?

谁说胖子不能爱 提交于 2019-12-04 03:14:28
I have this code in application controller : # Method to capture and handle all exceptions rescue_from Exception do |ex| Rails.logger.debug ex do_stuff(ex) end I want to move this into a module and then: class ApplicationController < ActionController::Base include 'module' ... Right now my module looks like: # lib/exception_mailer.rb require 'action_mailer' require 'active_support' module ExceptionMailer # Method to capture and handle all exceptions rescue_from Exception do |ex| ... And I am getting: undefined method 'rescue_from' for ExceptionMailer:Module I've googled 'how do i include

How can I convert JSON to XML in Ruby?

喜欢而已 提交于 2019-12-04 02:17:45
Is there any way to convert JSON to XML in Ruby? require 'active_support' #for to_xml() 'gem install activesupport' use the 2.3 branch require 'json' #part of ruby 1.9 but otherwise 'gem install json' my_json = "{\"test\":\"b\"}" my_xml = JSON.parse(my_json).to_xml(:root => :my_root) Also note the root argument of to_xml. If you don't specify a root it'll use the word 'hash' as the root which isn't very nice to look at. Regarding @rwilliams aka r-dub answer: ActiveSupport moved its components into separate modules for granularity. Rather than load everything all at once, we can tell it to load

rails activesupport notifications - wrong db runtime value

余生颓废 提交于 2019-12-04 01:44:33
I'm trying to log requests for my REST API application. I'm using rails notifications for this , like here http://railscasts.com/episodes/249-notifications-in-rails-3 I can't understand how to solve one problem with rails notifications. my initializer code ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload| p name p start p finish p id p payload end Controller respond section class PostsController < ApplicationController # GET /posts # GET /posts.json respond_to :json, :html .... end Controller create action def create @post = Post

converting Date object to TimeWithZone

ぐ巨炮叔叔 提交于 2019-12-03 19:34:26
问题 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)'].parse(date.to_s) => Wed, 17 Feb 2010 00:00:00 EST -05:00 >> ActiveSupport::TimeZone['UTC'].parse(date.to_s) => Wed, 17 Feb 2010 00:00:00 UTC 00:00 Is there a

What purpose can anonymous modules serve?

ぐ巨炮叔叔 提交于 2019-12-03 17:50:36
问题 What purpose could anonymous modules in a Ruby app serve? The concept itself is easy to grasp, but I can't imagine any reason that you'd ever use such a thing. What problem do they solve? 回答1: There is a more general principle at work here. Phil Karlton famously said: "There are only two hard problems computer science: cache invalidation and naming things." So, naming things is hard. Which means that if we can get away with not naming a thing, we should do it! Or, if you look at it from a

What do I need to do to get Hash.from_xml() to work?

半城伤御伤魂 提交于 2019-12-03 16:39:10
问题 I installed 'ActiveSupport' and required 'active_support' in my code, but I get a "No Method Error" when I try to use the Hash.from_xml() method. What am I missing? $ gem list returns: \*** LOCAL GEMS \*** activesupport (3.2.6) bundler (1.1.4) i18n (0.6.0) json (1.7.3) mime-types (1.19) multi_json (1.3.6) rake (0.9.2.2) rest-client (1.6.7) rubygems-bundler (1.0.3) rvm (1.11.3.5) And: $ ruby -v returns: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0] The contents of file.rb

Long nil safe method chains [closed]

与世无争的帅哥 提交于 2019-12-03 16:14:34
So I know of a few different approaches that I am aware of and I want explore the advantages and disadvantages of the various ways for various criteria which are: readability performance ease of debugging OO principles (low coupling and high cohesion) Explicitly using the try method from active support person.try(:pet).try(:name).try(:upcase) Using a rescue nil person.pet.name.upcase rescue nil Using an && operator chain person && person.pet && person.pet.name && person.pet.name.upcase Monkey patching the Object class, see https://gist.github.com/thegrubbsian/3499234 for the original gist

How to fix difference in behavior of activesupport 3.0.0 compare to 2.x?

半城伤御伤魂 提交于 2019-12-03 15:09:19
I am using Hash#to_xml in my Sinatra application. It did work till I moved to actviesupport 3.0.0 Is there a difference in usage of activesupport in 3.0.0? For example this works fine gem 'activesupport', '2.3.5' require 'active_support' {}.to_xml and gem 'activesupport', '3.0.0' require 'active_support' {}.to_xml generates: NoMethodError: undefined method `to_xml' for {}:Hash ActiveSupport no longer loads all its components when you require it. This allows you to cherry-pick the functionality that you want. require "active_support/core_ext/hash/conversions" {}.to_xml Or if you really want all