Rails vanity gem custom metric error: private method `to_time' called - ruby-on-rails

Hi I am trying to setup Vanity gem into a rail 3 application.
I am creating a custom metric to experiment a bit.
So I created it following the suggestions on their website:
metric "Signups" do
description "Signups completed"
def values(from, to)
(from..to).map { |i| 24 }
end
end
The file is located at the right place and is loaded, vanity picks it up but somehow it looks like there is an internal error in the way vanity works.
Of course: those values are only there for a testing purpose, they will be replaced by real ones later.
I get the following error when running this experiment
Thank you!

There seems to be an error in the gem:
in this file
.rvm/gems/ruby-1.9.2-p290/gems/vanity-1.7.1/lib/vanity/templates/_metric.erb
where we can read the following line
js = data.map { |date,value| "['#{**date.to_time.httpdate**}',#{value}]" }.join(",")
vanity_html_safe(%{<div class="chart"></div>
if we change date.to_time.httpdate to date.to_s.to_time.httpdate it works.

Related

Spree Commerce extending helpers

I am attempting to extend one of the helper modules which Spree Commerce offers. I came across the following file: spree/frontend/app/helpers/frontend_helper.rb
Ultimately what I am trying to do is add a simple helper method to this module. I plan to call this helper method for use with the single product page. My implementation seems to be working fine with a couple exceptions, which has led me to believe that I have screwed up somewhere. Here is what I have done:
I have the file app/helpers/spree/frontend_helper_decorator.rb. The contents of this file:
Spree::FrontendHelper.module_eval do
# Create Variant Matrix
def create_variant_matrix( variants )
#valid_variants = {}
...
#valid_variants
end
end
The next thing I have done is extended the Products Controller for the purpose of overwriting one of the methods. I created app/controllers/spree/products_controller_decorator.rb. The content of this file:
Spree::ProductsController.class_eval do
include Spree::FrontendHelper
# Overwrite Show to include variant matrix
def show
#variants = #product.variants_including_master.active(current_currency).includes([:option_values, :images])
#product_properties = #product.product_properties.includes(:property)
#taxon = Spree::Taxon.find(params[:taxon_id]) if params[:taxon_id]
#vMatrix = create_variant_matrix( #variants )
end
end
As you can see here, I have overwritten the show method, copied the code from the default spree method and added my custom call. I have also included the FrontendHelper module in the controller.
This works without any errors with few exceptions. When I run bundle for the purpose of installing a new gem for instance, I typically get an error which requires me to remove my custom FrontendHelper method completly before I can successfully install the gem. Once the gem is installed, I add the code back in and it works fine. The error I receive is:
/fake/path/app/controllers/spree/product_controller_decorator.rb:2:in `block in <top (required)>': uninitialized constant Spree::FrontendHelper (NameError)
I have tried searching for this error with no luck. I am fairly new to Ruby (background is in PHP), but based on the tutorials I followed, I feel that it is correct. It also seems to be working properly in most scenarios.
Any help is appreciated.

uninitialized constant UploadsController::Bucket when trying to write general AWS::S3 Upload controller

I'm trying to write a general Amazon S3 uploader (will be used mostly for images) for my rails project. I was able to set up the environment in my console following http://amazon.rubyforge.org/.
I was able to follow the guide in the console. However, I had trouble when I applied it to my rails project. When I try to access my new view, I get the following error:
NameError in UploadsController#new
uninitialized constant UploadsController::Bucket
Here is my controller:
class UploadsController < ApplicationController
require 'aws/s3'
def new
photo_bucket = Bucket.find('photos')
#photos = photo_bucket.objects
end
def create
file = 'black-flowers.jpg'
S3Object.store(file, open(file), 'photos')
end
end
In my controller, my new action will contain the form for the upload and a list of all the photos.
My create action will just save the file. I haven't figured out how the params from the form will be passed into the controller yet, so ignore the 'black-flowers.jpg' line.
My guess is that I did not establish a connection in the controller.
How do I establish a connection or fix this error?
Thanks for looking.
Bucket is not a top level constant in this case. You probably want the AWS::S3::Bucket constant for new, and I'd assume something similar for S3Object.
Note that you may also want to look into the Fog library for this.
The fact that you haven't figured out how params will be passed in implies that you may also want to work through the Rails tutorials without S3 first.
I had a similar issue and it was solved by just checking all files required were provided and restarting the server

general setup for searchify in rails, how to access variable from initializer file

how do i access a variable from an initializer file?
i have a file called search.rb in my initializer folder
api_client = IndexTank::Client.new 'http://:xxyyzz#xv9v.api.searchify.com'
index = api_client.indexes 'idx'
however, in my controller whenever im trying to index a newly created lesson, rails gives me an error of
undefined method `document' for nil:NilClass
my controller is...
def create
index.document(#lesson.id).add({:text => #lesson.content })
end
also is this a bad way of indexing my documents? whenever they're being created? thanks
You might find it useful to look at the Tanker gem by kidpollo - https://github.com/kidpollo/tanker
It's a 3rd party IndexTank gem for rails. You don't actually have to use it - you can keep using the indextank gem if you want. But looking at the source of the Tanker gem may be helpful in figuring out the best way to write your code under rails.
Looks like index object was not initialized.
NO, it isn't a bad way for indexing your documents.
But, I suggest to move indexing in special Index model which will handle all indexing logic for the particular index.
Think of index like of database table.

Ruby on Rails match routing error

I'm trying to make a Rails application that serves simple static HTML pages. I followed Mikel's tutorial here (it involves making a Pages controller and setting up some routing) but I keep getting an error message.
I made a app/views/site/pages/_about.html.erb file to contain my About page. After starting the rails server, I try to go to http://localhost:3000/about/ but it gives me a Routing Error because I have an "uninitialized constant Site."
My project is uploaded to GitHub if you want to take a look at the code.
Edit: here's my config/routes.rb file:
NINAgallery::Application.routes.draw do
match ':page_name' => 'site/pages#show'
end
And here's the important part of my app/controllers/pages_controller.rb file:
class PagesController < ApplicationController
layout 'site'
def show
#page_name = params[:page_name].to_s.gsub(/\W/,'')
unless partial_exists?(#page_name)
render 'missing', :status => 404
end
end
# extra code for handling 404 errors goes here
end
site/pages#show means the show action in Site::PagesController
You either need to put your controller in the namespace your routes imply or change the route
The last line in the PagesController is this:
ValidPartials = Site::PagesController.find_partials
That means that the PagesController is contained in a Site module. But there is no Site module in your app.
I think simple removing Site:: should fix the problem:
ValidPartials = PagesController.find_partials
Plus the route:
match ':page_name' => 'pages#show'
Your application is called NINAgallery.
Replace Site in pages_controller.rb line 27 by NINAgallery.
PS:
I just took a peek at the so-called tutorial. You are taking really really really bad habits.
Some resources to take very good basics:
http://guides.rubyonrails.org/
http://api.rubyonrails.org/
If you like tutorials: http://ruby.railstutorial.org/
And there are plenty of books about rails. All good.
Besides the namespace problem, you also needed to add the 'app' Gem to the Gemfile, as explained in the tutorial.
I don't know why you removed the caching of the static pages in your working code. I made a pull request with the app working and maintaining the cache problem. If another person is interested, the code is here
Also ryan bates has a tutorial called "Semi static pages" that does something similar. I would encourage you to follow his solutions because there are very rarely mistaken.

memcache error illegal character in key (Ruby 1.8.7 / Rails 2.3.9)

I am getting the following error in one of my rails app [Ruby 1.8.7 + Rails 2.3.9]
A ArgumentError occurred in home#dashboard:
illegal character in key "dashboard_prod:views/reports/1050 - 097"
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/activesupport-2.3.9/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb:643:in `get_server_for_key'
I googled and found that someone had similar problem at: http://www.coffeepowered.net/page/2/
on that page it is mentioned that, this should work:
class ActionController::Caching::Actions::ActionCachePath
def path
#cached_path ||= Digest::SHA1.hexdigest(#path)
end
end
But I am not sure where should I type this. So I have two questions:
How to solve problem at hand
Where should I write the code like the above where we are overriding some standard class or class defined in a Gem.
Any help would be appreciated.
I think the post you found is suggesting you create a monkey patch with that code. Create a file under Rails.root + 'lib/' with those contents, and make sure it loads after ActionController (which should be the default). The patch will override ActionController's default code.
You definitely want something like that--I always ensure my memcached keys are hashed. It makes them a little more difficult to debug, but it protects against problems like this and also key-length overflow errors when someone creates a key that's too long for memcached.

Resources