activesupport

How to use NSTimeZone -timeZoneWithName: with a city name from Rails ActiveSupport?

依然范特西╮ 提交于 2019-12-18 14:14:53
问题 If I only have the city name like Bangkok or Tokyo , how can I supply a timezone parameter in [NSTimeZone timeZoneWithName:@"Asia/Tokyo"] where it also has continent and slash in front of city? I already tried [NSTimeZone timeZoneWithName:@"Tokyo"] , it doesn't work. 回答1: Thanks for the answers guys, but it looks like those city names are just a format that Rails "ActiveSupport::TimeZone" uses. So I just have to map it back. The mapping is here -> http://api.rubyonrails.org/classes

How to Convert TZInfo identifier to Rails TimeZone name/key

早过忘川 提交于 2019-12-18 11:09:56
问题 How do you convert js values received as TZInfo identifiers to Rails TimeZone name/key? FROM: "America/New_York" returned from JavaScript TZinfo detection TO: "Eastern Time (US & Canada)" convention used in Rails TimeZone or another example: "Pacific/Honolulu" => converted to => "Hawaii" Both are available in ActiveSupport::TimeZone < Object mapping but rails uses the key [i.g. "Eastern Time (US & Canada)" ] in drop-downs, validation & storing to Time.use_zone() . Based on what I understand

Use Rails 3's ActiveSupport core extensions outside rails

安稳与你 提交于 2019-12-18 10:26:36
问题 I'm having a problem using ActiveSupport's core extensions on a gem I am developing. I had it working with AS 2.3.8, but as soon as I wanted to port it to 3b4, the extensions stopped working and my test results are filled with lines such as: undefined method `blank?' for "something":String I've included it via gem "activesupport" followed by require "active_support" Is there anything else I need to call to include those extensions? Thanks 回答1: ActiveSupport is more separate now in Rails 3. If

Rails ActiveSupport: How to assert that an error is raised?

六眼飞鱼酱① 提交于 2019-12-18 03:01:25
问题 I am wanting to test a function on one of my models that throws specific errors. The function looks something like this: def merge(release_to_delete) raise "Can't merge a release with itself!" if( self.id == release_to_delete.id ) raise "Can only merge releases by the same artist" if( self.artist != release_to_delete.artist ) #actual merge code here end Now I want to do an assert that when I call this function with a parameter that causes each of those exceptions, that the exceptions actually

Rails ActiveSupport Time Parsing?

荒凉一梦 提交于 2019-12-17 17:29:10
问题 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? Something like Time.parse_formatted_s(:db) which would parse a string in Database format, returning a new Time object. This seems like something that rails

How to add 10 days to current time in Rails

岁酱吖の 提交于 2019-12-17 15:37:11
问题 I tried doing something like Time.now + 5.days but that doesn't work, even though I vaguely remember seeing, and being very impressed, with being able to do something like 2.years etc. How do I do that in Rails 3? 回答1: Use Time.now + 10.days or even 10.days.from_now Both definitely work. Are you sure you're in Rails and not just Ruby? If you definitely are in Rails, where are you trying to run this from? Note that Active Support has to be loaded. 回答2: days , years , etc., are part of Active

How do I use Active Support core extensions?

有些话、适合烂在心里 提交于 2019-12-17 08:17:44
问题 I have Active Support 3.0.3 installed and Rails 3.0.3 with Ruby 1.8.7. When I try to use 1.week.ago I get NoMethodError: undefined method 'week' for 1:Fixnum from (irb):2 The other core extensions seem to work. I tried it on a friend's computer (same install specs and legacy versions are on his) with the same results. What gives? All of this is in IRB. 回答1: Since using Rails should handle this automatically I'm going to assume you're trying to add Active Support to a non-Rails script. Read

ActiveSupport::TimeZone not recognized in Rspec tests

谁都会走 提交于 2019-12-13 21:38:36
问题 I am using ActiveSupport::TimeZone to set the time zone on a location based on the zip code. def set_time_zone self.time_zone = ActiveSupport::TimeZone.find_by_zipcode(self.zip) end This works just fine in the application itself. I am calling the set_time_zone on before_save. In running the tests with Rspec, when it tries to run the set_time_zone method it errors out with "undefined method 'find_by_zipcode'in ActiveSupport::TimeZone" I have included "require 'active_support/time_with_zone'"

can't load require_dependency

半城伤御伤魂 提交于 2019-12-13 16:17:59
问题 I'm trying to use require_dependency from ActiveSupport library in pry: require 'active_support' #=> true require_dependency 'test' #=> NoMethodError: undefined method #=> `require_dependency' for main:Object What could be the problem? 回答1: ActiveSupport used to be pretty evil by loading a ton of stuff on require. The "kitchen sink" approach opened a lot of core classes up and changed their behavior (like JSON). This caused incompatibilities/problems with other gems and code that expected

How can I convert JSON to XML in Ruby?

北战南征 提交于 2019-12-13 12:00:20
问题 Is there any way to convert JSON to XML in Ruby? 回答1: 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. 回答2: Regarding @rwilliams aka r-dub answer: ActiveSupport moved its