Christophe Estanol

Joined

390 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Searchick filter with scope

Well that was easy! Thanks very much Chris.

Posted in Nofications screen cast

I had a quick look at your repository and it might not be up to date because there are several things missing to create notifications.
For example the coffeescript https://github.com/excid3/gorails-episode-96/blob/master/app/assets/javascripts/notifications.js.coffee
Or the jbuilder for the Json https://github.com/excid3/gorails-episode-96/blob/master/app/views/notifications/index.json.jbuilder
Or the controller https://github.com/excid3/gorails-episode-96/blob/master/app/controllers/notifications_controller.rb

Have a look at this example that Chris provided: https://github.com/excid3/gorails-episode-96

Posted in Searchick filter with scope

Thanks for your help Chris.

The query was initially working fine but returning all records even the ones that are not "Published" so I figure .published was not working.

class Article < ActiveRecord::Base

  searchkick highlight: [:title], text_start: [:title], language: "spanish"

  scope :draft,     ->{ where(status: "Draft") }
  scope :published, ->{ where(status: "Published") }
  scope :scheduled, ->{ where(status: "Scheduled") }

  def search_data
    {
      title: title,
      content: content
    }
  end

I am searching for a simple word that should be returning many records that are "Published". If I remove where:{status: "Published"} the query works again but including all records.
Here is the output from rails c :

Article.search("Amor", {where:{status: "Published"}, operator: "or", suggest: true})
  Article Search (6.0ms)  curl http://localhost:9200/articles_development/_search?pretty -d '{"query":{"filtered":{"query":{"dis_max":{"queries":[{"match":{"_all":{"query":"Amor","operator":"or","boost":10,"cutoff_frequency":0.001,"analyzer":"searchkick_search"}}},{"match":{"_all":{"query":"Amor","operator":"or","boost":10,"cutoff_frequency":0.001,"analyzer":"searchkick_search2"}}},{"match":{"_all":{"query":"Amor","operator":"or","boost":1,"analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"_all":{"query":"Amor","operator":"or","boost":1,"analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"filter":{"and":[{"term":{"status":"Published"}}]}}},"size":1000,"from":0,"fields":[]}'
 => #<Searchkick::Results:0x007f9228714280 @klass=Article(id: integer, title: string, author: string, content: text, introduction: text, description: text, facebook: boolean, twitter: boolean, published_at: datetime, status: string, error: text, views_count: integer, created_at: datetime, updated_at: datetime, image_file_name: string, image_content_type: string, image_file_size: integer, image_updated_at: datetime, slug: string, user_id: integer), @response={"took"=>3, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}, @options={:page=>1, :per_page=>1000, :padding=>0, :load=>true, :includes=>nil, :json=>false, :match_suffix=>"analyzed", :highlighted_fields=>[]}>

I did try to reindex without seeing any changes.

Posted in Searchick filter with scope

Has anybody used searchkick with a scoped model?

I have an Article model with scope :published, ->{ where(status: "Published") }.
So In my ArticleController I did the following:

query = params[:q].presence || "*"
@search = Article.published.search(query, operator: "or", suggest: true)

but it didn't work https://github.com/ankane/searchkick/issues/140 and kept including all the records

So I did:

query = params[:q].presence || "*"
@search = Article.search(query, where:{status: "Published"}, operator: "or", suggest: true)

Something like http://stackoverflow.com/questions/34797144/how-to-use-scopes-with-searchkick-in-controller
Didn't work either... and returned nothing.
Not sure what I am doing wrong.

Posted in Polling/Long Polling navbar notifications

Chris, Thank you for taking the time to explain in details each points mentioned.
I now understand better why you've used regular interval polling.

Posted in Polling/Long Polling navbar notifications

Chris I have been using your implementation of navbar notifications and I really like the way it's constructed.
I can implement Facebook style notifications and I am pretty happy about it.
However I have a couple of questions for you:

1 - I was reading about polling and long polling techniques for example this article
http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery.
And as far as I can understand you are using simple polling and not long polling. Is that right? And why not using the other techniques.

2 - Also, I always find rails incredibly fast but with the notifications I feel that the load generated (in my app, a request every 5 sec) slows down the response time.

3 - And finally the console is full of requests every few seconds and that makes it difficult for debug. Even Better error is kind of acting funny where the shell included doesn't respond well and throw "Session expired" error at times. Is there a way to improve that?

I may well have done something wrong and experience some weird things.

Maybe in a future episode you could implement pub/sub or Websocket notifications.

That's it, just curious about your thoughts on the matter.

Thanks again for you work.

Posted in About In-App Navbar Notifications

So far it's been easy to implement the first approach.
I will look into refactoring when the MVP gets done. Thanks for the tips.

Posted in About In-App Navbar Notifications

Chris, thanks for this one. Notifications work like a charm.

I was just wondering...

How do I create different JSONs for different models?

json.array! @notifications do |notification|
  json.id notification.id
  #json.recipient notification.recipient
  json.actor notification.actor.username
  json.action notification.action
  json.notifiable do #notification.notifiable
    json.type "a #{notification.notifiable.class.to_s.underscore.humanize.downcase}"
  end
  json.url forum_thread_path(notification.notifiable.forum_thread, anchor: dom_id(notification.notifiable))
end

Let say I want to send admin a notification every time somebody creates a thread, for example.

Or maybe in your case you receive a notification every time you have a new subscriber.

How do I create the url for every model?

Posted in Sharing on social network

There is an interesting thread in stackoverflow about this "issue":

http://stackoverflow.com/questions/19463978/rails-4-and-turbolinks-meta-tags-not-changing

This "issue" has been raised and shot down a couple times, each time being declared a non-issue by DHH himself, so I wouldn't count on this behavior changing any time soon.

In my case as I needed a quick fix I traded

<!-- content for Open Graph and twitter Cards -->
<% content_for :title, @article.title %>
<% content_for :description, @article.description %>
<% content_for :url, article_url(@article) %>
<% content_for :image, @article.image.url(:medium) %>

for

<script type="text/javascript">
  $(document).on('ready page:change', function() {
    $("meta[name='twitter\\:title']").attr("content", "<%= @article.title %>");
    $("meta[name='twitter\\:description']").attr("content", "<%= @article.description %>");
    $("meta[name='twitter\\:image']").attr("content", "<%= @article.image.url(:medium) %>");
    $("meta[property='og\\:title']").attr("content", "<%= @article.title %>");
    $("meta[property='og\\:description']").attr("content", "<%= @article.description %>");
    $("meta[property='og\\:url']").attr("content", "<%= article_url(@article) %>");
    $("meta[property='og\\:image']").attr("content", "<%= @article.image.url(:medium) %>");
  });
</script>

So far it seems to be working fine.

Posted in Sharing on social network

Chris just a quick one.

I also use <% yield %> to dynamically change the <title> meta tag and it works perfectly.
Would you know why <meta> tag doesn't work and <title> works?
It should work the same way...

Posted in Sharing on social network

I will try and do that.
It will be an interesting challenge.
And I'll be back with the answer whenever I manage to get it working.
Thanks Chris, good job for keeping up with the questions.

Posted in Sharing on social network

Well I don't see any reason it shouldn't work.
I could use this workaround as it's fired on ready page:change but I don't really see how to update the meta tags from my content/yield...

Posted in Sharing on social network

Yes it's working partially.

I didn't realized it either until I used AddThis widget to expand the scope of possibilities to share. For example AddThis allow to share on whatsapp which is pretty nice with mobile use.

Implementing AddThis with this workaround worked fine with Turbolinks until I noticed a discrepancy between my share button and the one AddThis provide.

So in this case I am not sure what I can do, and I wouldn't deactivate Turbolinks completely as it really provides this single page app fluidity.

Posted in Sharing on social network

Hey Chris, I have been using option #3 and the <% content/yield %> method you suggested but I suspect that Turbolinks is acting up. The share buttons for Facebook and Twitter are working perfectly

<li class="btn-social"><a class="js-social-share facebook" href="https://www.facebook.com/sharer/sharer.php?u=<%= yield :url %>" target="_blank"><i class="fa fa-facebook-official" aria-hidden="true"></i></a>
<li class="btn-social"><a class="js-social-share twitter" href="https://twitter.com/intent/tweet?text=“<%= yield :title %>”&url=<%= yield :url %>&via=twitter" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a>

However if I inspect the markup the meta tags aren't actually up to date unless I manually refresh the page. In other words Turbolinks doesn't allow the content/yield to take place.

Here are my meta tags as you suggested:

<!-- Twitter cards -->
  <meta name="twitter:card" content="summary_large_image">
  <meta name="twitter:site" content="@me">
  <% if content_for?(:title) %>
  <meta name="twitter:title" content="<%= yield :title %>">
  <% else %>
  <meta name="twitter:title" content="Something else">
  <% end %>

I was wondering how you might have solved that yourself.

Thanks Chris for your suggestion.
You were absolutely right. I removed the callbacks and did it explicitly in the controller and it worked.
I have created a gist with the code I used.
Hopefully someone will recycle it, use it, make it better.
https://gist.github.com/ChrisEstanol/acb0c2883d995d74c498

Posted in Rich text format editor in rails 4

Since I started to blog with Jekyll, Markdown seem to be the most convenient way to edit text.
I have used an editor like the one used in the Ghost blogging platform for a client rails project and it worked perfectly.
Maybe worth considering in the future https://github.com/timsayshey/Ghost-Markdown-Editor.
I find it way easier to use that any text editor.

Posted in Sharing on social network

Thanks Chris for the suggestion!

Following Chris great post on scheduling I am using delayed_job_active_record to schedule quote from my app to be tweeted and shared on FB.

Here is my quote.rb model with the current logic:

      # == Schema Information
      #
      # Table name: quotes
      #
      #  id           :integer          not null, primary key
      #  content      :text
      #  author       :string
      #  created_at   :datetime         not null
      #  updated_at   :datetime         not null
      #  published_at :datetime
      #  status       :string
      #  facebook     :boolean
      #  twitter      :boolean
      #  user_id      :integer
      #  error        :text
      #

      class Quote < ActiveRecord::Base

        belongs_to :user
        validates :content, presence: true
        validates :author, presence: true
        # validation using validates_timeliness gem
        # validates_datetime :published_at, :on => :create, :on_or_after => Time.zone.now
        # validates_datetime :published_at, :on => :update, :on_or_after => Time.zone.now
        after_save :schedule

        scope :draft,     ->{ where(status: "Draft") }
        scope :published, ->{ where(status: "Published") }
        scope :scheduled, ->{ where(status: "Scheduled") }

        before_validation :clean_up_status

        def clean_up_status
          self.published_at = case status
                              when "Draft"
                                nil
                              when "Published"
                                Time.zone.now
                              else
                                published_at
                              end
          true
        end

        def schedule
          puts "!!!!!!!!!!!!!!!#{self.status}!!!!!!!!!!!!!!!"
          if self.status == "Scheduled"
            begin
                ScheduleJob.set(wait_until: published_at).perform_later(self)
            rescue Exception => e
                self.update_attributes(status: "Scheduling-error", error: e.message)
            end
          else
            publish
          end
        end

        def publish
          unless self.status == "Draft"
            begin
                if self.facebook
                    to_facebook
                end
                if self.twitter
                    to_twitter
                end
              self.update_attributes(status: "Published")
            rescue Exception => e
              self.update_attributes(status: "Publishing-error", error: e.message)
            end
          end
        end

        def to_twitter
          client = Twitter::REST::Client.new do |config|
            config.consumer_key        = ENV['TWITTER_KEY']
            config.consumer_secret     = ENV['TWITTER_SECRET']
            config.access_token        = self.user.twitter.oauth_token
            config.access_token_secret = self.user.twitter.secret
          end
          client.update(self.content)
        end

        def to_facebook
          graph = Koala::Facebook::API.new(self.user.facebook.oauth_token)
          graph.put_connections("me", "feed", message: self.content)
        end
      end

I have tried before_create :schedule and before_update :schedule before before_save and none of them seem to work.

My jobs/schedule_job.rb:

    class ScheduleJob < ActiveJob::Base
     queue_as :default

     def perform(quote)
       quote.publish
     end
    end

With basic config: initializers/delayed_job_config.rb that have also added after the error:

    Delayed::Worker.destroy_failed_jobs = false
    Delayed::Worker.max_attempts = 1

Whenever I schedule a quote delayed_job enter in an infinite loop, eventually publish the quote and keep on runnig in circle.

Here is the log:

    rake jobs:work
    [Worker(host:Christophes-MacBook-Pro.local pid:8962)] Starting job worker
    [Worker(host:Christophes-MacBook-Pro.local pid:8962)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=7) RUNNING
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Published!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!

Any idea what could I be missing? I am using Puma for dev and prod server.
I also created a question in stackoverflow. http://stackoverflow.com/questions/35206588/delayed-job-infinite-loop
Whenever I manage to make it work I will share a gist so it can be used.

Posted in Sharing on social network

What is the best way for the visitors of my page (not logged in) to share models data (for example articles and courses) on Facebook and Twitter.
I have been looking for an answer without really finding a good implementation.
There are some gems:

1 - Should I implement a basic Javascript library
2 - Strip down these gems and build a custom solution based on those
3 - Simply use sharer url like https://www.facebook.com/sharer/sharer.php?
4 - Use Twitter and Koala gem

Also if I want to include sharing buttons for multiple models (for example articles and courses) on the same page how can I dynamically change my open graph and twitter card so that it reflect the correct model data:

<% content_for :head do %>
    <!-- Open Graph tags -->
    <meta property="og:title" content="<%= @article.title %>" />
    <meta property="og:type" content="article" />
    <meta property="og:url" content="<%= article_url(@article) %>" />
    <meta property="og:description" content="<%= @article.body %>" />
      <meta property="og:image" content="<%= absolute_image_url(img.attachment.url) %>" />
    <meta property="fb:app_id" content="12345678" />
    <!-- Twitter card property -->
    <meta name="twitter:card" content="<%= @article.title %>">
    <meta name="twitter:site" content="@mytwitter">
  <% end %> 

Thanks for reading