Chris Oliver

Joined

292,890 Experience
93 Lessons Completed
295 Questions Solved

Activity

Hey Stan,

You'd actually just put an if statement in the JS. If the length of the notifications > 0, display them, else, render your placeholder.

Something like this (I haven't tested this, so it may have some bugs):

  handleSuccess: (data) =>
    items = $.map data, (notification) ->
      "<a class='dropdown-item' href='#{notification.url}'>#{notification.actor} #{notification.action} #{notification.notifiable.type}</a>"

    $("[data-behavior='unread-count']").text(items.length)

    if items.length == 0
      items = "<a>No new notifications</a>"

    $("[data-behavior='notification-items']").html(items)

Something like this should fix it:

desc "Fetch anime info"
task :fetch_anime_info => :environment do
 require "open-uri"
 require 'mechanize'

 Anime.all.each do |anime|
  season_scrape = doc.css('div#content .borderClass .js-scrollfix-bottom div:contains("Premiered")').text.split(' ')[1..-1]
  season_text = season_scrape.blank? ? "" : season_scrape.join(' ')

  season = Season.where(name: season_text).first_or_create
  anime.update(season: season)
 end
end

This way you'll look up the season to see if it exists, otherwise create a new one, and then you'll associate the anime to the season.

Posted in Limit CSV export by Day, Week and Month

Hey Sacha, you would just want to use a where query to scope to a date range before calling to_csv on it. You may need to change it from all.each to scoped.each in order for Rails to respect the scope if it wants to override your where query. That should do it!

Updating the season through the association works well.

And a side note: That url with the UUID at the end is what happens when friendly_id already detects a duplicate.

Posted in Multitenancy with the Apartment gem Discussion

That's a good question. Aside from a brute force approach, I'm not sure. What I would probably do is just query for the record, make a copy of the object with .dup, switch the tenant to the new one, save the duplicate in the new tenant, verify it exists, switch back to the old tenant and delete the old record (if necessary).

Always something like that, cascading into a bug showing up elsewhere. :) Glad you got it fixed!

Oh interesting. In that case, is your stripeToken nil?

First I would check to make sure that the user has a stripe_subscription_id saved in the record. You might be passing a nil in on accident.

Posted in Inviting Users with devise_invitable Discussion

That's actually my cat. :) He does some chirp-y sounding meows when he sees things outside like birds or squirrels. I'll have to let him in one of the videos soon.

Posted in PDF Receipts Discussion

I definitely will and thanks for the support! :D

Posted in PDF Receipts Discussion

Yeah! So normally with subscriptions, I listen to the charge.created webhook and save a copy of it to the database as a Charge record. That's like the example I use. The reason for needing the webhook is that subscriptions charge the user monthly and they don't have to initiate anything in your app.

For one-time charges, you can create a Charge object immediately during checkout and use that.

And if you want to store the receipt PDF files instead of generating them dynamically each time, you can save the file it generates to S3 and save it using Shrine or Carrierwave, etc and just link to that file from your view.

Posted in How to build REST APIs?

Ali, great suggestion. I definitely will do that. There are only a handful of important concepts you really need to know, and I agree, I haven't seen many videos that actually cover this from a high level. I'll make sure I get to this soon!

Posted in Create a User Profile after saving a Devise User

  1. When the user registers, do you want to have them fill out some of the profile information as well? If so, then I would recommend updating the Devise form to be a nested form. That way you create both together at once using accepts_nested_attributes_for.

  2. When Devise creates a user, it does actually sign them in as soon as they're persisted to the database (it would be weird if you registered and then had to login immediately).

  3. Depends on what you want to accomplish really. If you want two things filled out at once, the above solution in #1 is great and pretty painless.

The other option is just to create a blank profile during the registration process if that's what you're looking for. In this case, I would override the Devise controller, copy the create action, and then add in code on the save part to also create and save your profile for the resource.

For example, you could modify the default create action to look like this which would provide an atomic method of creating both the user and the profile:

class Devise::RegistrationsController
  def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?

      # We know that the user has been persisted to the database, so now we can create our empty profile
      resource.create_profile!

      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
    end
  end
end
``

Posted in data consistency for invitation registration

You got it man! 👍 🎉

You're welcome Nick! :D

I think the current_period_end timestamp on the Stripe Subscription is what you want there. You could cache this on the user so you don't have to hit their API every time.

You don't necessarily always want to redirect to that same page, this way you can specify it as you want. Of course, you can set it up that way to always set a cookie. Depends on what you're looking to accomplish really.

You could change it to just check to see if there are question marks in the text at all instead.

if line.text.include?("?")

For example, something like this (assuming that's the string):

page.css('span.remain-time').each do |line|
  if line.text == "???,??,???"
    date = "No date"
  else
   n = 3
   d = line.text[/(\S+\s+){#{n}}/].strip
   date = Date.parse(d)
  end

  puts date
end