codebar/mentoring.io — pull request #14 by Rosa-Fox (2014-10-24 18:02:48+00:00)
Title: Twitter share

Hi all,

Don't merge this yet as it is not ready to go to production!

The aim is that a `Share on Twitter button` appears on all the classified ads that have been posted. Users will be able to share adverts on Twitter and people will be able to view the posted URL even if they are not signed up to the application. There is a link to `Sign up as a mentor` on the advert to try and pursuade/make it easier for people to sign up.

This works...

BUT

1. At the moment anyone can post anyone else's advert. I thought this could potentially be a good thing as it will spread the word, but until we implement the feature that will allow us to disable classifieds this could cause problems, say for example if that person has already found a mentor... people might get annoyed if they are trying to help someone who is already being helped. I am not too sure how to go about letting people choose if they wanted their adverts to be shared. I thought a boolean `share` field could get added to the Classifieds table but I wasn't sure how this would work with the `before_save` hook? Guessing I could do a conditional for the `share` boolean in the `add_url_token` method which is in the `before_save`?

2. The URL that is dispensed to the public has a generated token in it to try and make it as anonymous as possible (aka `http://localhost:3000/classifieds/4BB695F`). This works fine, but I had to delete all of the data from my tables and start again for stuff to display in the view. This is because there were all the old records in there that didn't have generated tokens. I don't know how this would work going to production? Would we need a rake task that gives all the existing Classifeds a generated token?

3. The URL that is passed into the tweet. There must be a better way! But I couldn't figure out how to get it into the html for the tweet using the path alone `classified_path(classified.url_token)` as it needs to be prefixed with http:// in order to display in the tweet. I have done this but obviously it would be different in development and production:

```
- url = "http://localhost:3000"
- classified_path = classified_path(classified.url_token)
- if classified.preview == false
%a.twitter-share-button{"data-hashtags" => "mentoring.io", "data-size" => "large", "data-text" => "Could you be my mentor? Checkout what I would like to learn here:", "data-url" => url + classified_path, :href => "https://twitter.com/share"} Tweet
:javascript
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');
```

So yeah if you want to have a look at it then delete all the records out of your db `Member.delete_all` and `Classified.delete_all` and create some new people

To get the seed data back `rake db:drop db:create db:migrate db:seed`

Any advice much appreciated!