Customizing the label of vcard - ruby-on-rails-3

I am using gem "vcard". How can I customize the name of URL to read it as Skype/linkedin.
My code is like
card = Vcard::Vcard::Maker.make2 do |maker|
maker.add_url(linkedin_url) if linkedin_url
end
This is coming within "other" in card. I want this "other" to come as "linkedin".

card = Vcard::Vcard::Maker.make2 do |maker|
maker.add_field(::Vcard::DirectoryInfo::Field.create("item1.URL", '', "type" => "pref:linked in_url value"))
maker.add_field(::Vcard::DirectoryInfo::Field.create("item1.X-ABLabel", 'Linkedin Url'))
end

Related

Generating report in pdf using pdfkit on the basis of some search parameter

I am new to rails. I want to generate a pdf so for that I have used pdfkit gem. I followed the railcast tutorial but the problem in my case is that first I am searching for the record between the two dates than it redirects to a page and I have to print that page into pdf. The page that i have to generate pdf have some parameters in the address bar like this : http://localhost:3000/report?utf8=%E2%9C%93&mer_report%5Bmerchant_id%5D=32&sdate=2015-06-13&edate=2015-10-13
Help me how i will do that.
View :
<p><%= link_to "Download", report_path %></p>
Controller :
merchant_id = params[:mer_report][:merchant_id].to_i
merchant = Merchant.find(merchant_id)
$merchant = merchant
#merchant_id = merchant.id
#start_date = params[:sdate]
#end_date = params[:edate]
#original_merchant_name = merchant.name
#total_customer = merchant.users
#total_visits = merchant.visits.where(checked_in: true)
application.rb :
config.middleware.use "PDFKit::Middleware", :print_media_type => true

Find coordinates for my database's address column

I have my own database that includes: address, lat, long, name.
I have all the addresses that I want without their coordinates. Is there a way to find the coordinates in order to use them as markers on my active map?
In other words I want something to batch?! geocode my addresses that are already in to my database.
Thank you very much
Edited:
def index
if params[:search].present?
#locations = Location.near(params[:search], 20, :order => :distance)
else
#locations = Location.all
#locations.each do |l|
if l.latitude.nil?
new_location = "#{l.address}"
s = Geocoder.search(new_location)
l.latitude = s[0].latitude
l.longitude = s[0].longitude
l.save
end
end
end
This is what I have, but it updates only my first address on the database.
Check out the gmaps4rails gem at https://github.com/apneadiving/Google-Maps-for-Rails
Here's what I use in the model one of my apps:
acts_as_gmappable lat: 'latitude', lng: 'longitude',
process_geocoding: :geocode?,
address: :full_address,
msg: "Sorry, not even Google could figure out where that is"
Oh, and here's my geocode? method:
def geocode?
latitude.blank? || longitude.blank?
end
So when the model's latitude and/or longitude are nil, gmaps4rails automatically does a lookup and fills in the entries.

Process form data before creating table entry

Backstory: I'm building a site that takes in a Soundcloud URL as part of a post. Currently, I store the link they provide and, when a user loads their feed view, I retrieve the associated image / title / favorite count etc. via my post_helper. I have quickly come to realize that this is not scalable and is hurting load times.
So, what I think I should do (feel free to tell me that there is a better way), is to retrieve the SC/YT metadata on form submit and store it along with the other post data (id, user, content etc.) in the posts' table entry. How would I go about calling the helper methods to retrieve such on form submit and include the metadata in the submitted params?
post_helper.rb excerpt:
def soundcloud_info(soundcloud_url, type)
begin
resolve = scClient.get('/resolve', :url => soundcloud_url)
track_info = scClient.get("/tracks/#{resolve.id}")
rescue Soundcloud::ResponseError => e
%Q{ Error: #{e.message}, Status Code: #{e.response.code} }
end
if type == "title"
%Q{#{track_info['title']}}
elsif type == "image"
%Q{#{track_info['artwork_url']}}
elsif type == "favCount"
%Q{Favorite count: #{track_info['favoritings_count']}}
end
end
post_controler.rb excerpt:
def create
#post = current_user.posts.build(params[:post])
if #post.save
flash[:success] = "Your post was successful!"
redirect_to root_url
else
#feed_items = current_user.feed.paginate(page: params[:page])
render 'static_pages/home'
end
end
So apparently it's pretty straight forward... all I need to do is modify the parameters in the controller before I call #post = current_user.posts.build(params[:post]). My issue was that I was trying to do so in the helper.
I haven't quite adapted the whole thing to get all my required fields, but here's an example of how I have adapted the create method to pull the api URL out if someone submits SoundCloud's embed iframe.
micropost_controller.rb excerpt:
def create
#url = params[:post][:link_html]
if #url[/^.*src="(https|http):\/\/w.soundcloud.com\/player\/\?url=(.*)">/]
params[:post][:link_html] = CGI::unescape($2)
end
#post = current_user.posts.build(params[:post])
if #post.save
flash[:success] = "Your post was successful!"
redirect_to root_url
else
#feed_items = current_user.feed.paginate(page: params[:page])
render 'static_pages/home'
end
end

Rails 3 - routing and parameterized name

I use this rule in my model:
def to_param
"#{self.name.parameterize}"
end
and in my helper:
def articles_menu
menu = '<ul>'
Article.all.each do |article|
menu += '<li>'
menu += link_to "#{article.name}", article
menu += '</li>'
end
menu += '</ul>'
return menu.html_safe
end
but when I'll go to the /articles/my-new-flat, I'll get the error
ActiveRecord::RecordNotFound in ArticlesController#show
Couldn't find Article with id=my-new-flat
Missing I something else yet or is any problem in my app? I though for parameterizing of the name is needed only set up in the model the rule...
Use FriendlyId gem it has all what you need, here is the link https://github.com/norman/friendly_id/blob/master/README.md
Problem with your code is that rails under the hub is trying to query your model by "id" attribute, it is not querying "name" attribute. FriendlyId gem patches this for models you want

How to query a collection of embedded mongo documents to extract specific document with a list of criteria?

I am trying to select a collection of document based on the content of their embedded documents.
My model looks like this:
class box
embeds_many :items
field :stuff
end
class item
field :attrib1
field :attrib2
field :array
end
So with this structure I can query with the following to extract a collection of boxes bases on it's item's attributes:
Box.any_in(:'items.array' => [:value1, :value2]).where(:'items.attrib1'=> 'x', :'items.attrib2' => 'y').order_by([:stuff, :asc])
So this query gives me a collection of box that contains items with attributes 1 = x and attributes 2 = y and array that contains value1 or value2
This is all great, but the problem is that I need to tie up all the attributes into 1 item. What I mean is that this query will return me box like this:
box
{
items
[
{array => [value1], attrib1 => "x", attrib2 => "z"}
{array => [value1], attrib1 => "h", attrib2 => "y"}
]
}
The criteria's of the query are respected because it's true that attrib1 = 'x' and attrib2 = 'y' in that box, but unfortunately not within the same item.
That's what I need, the list of boxes contains items that have all the desired values within the same item.
How can I do that ? I have just no idea ? I hope I made myself clear, I wasn't really sure how to explain my problem
Thanks,
Alex
I found a beginning of an answer here:
http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29#DotNotation%28ReachingintoObjects%29-Matchingwith%24elemMatch
Now I need to figure out how to do this in mongoid...
Alex