SageBeard / blockipedia

0 stars 1 forks source link

private wiki #9

Closed SageBeard closed 7 years ago

SageBeard commented 7 years ago

I've set up my Wikis so the premium user can make Wikis private and the standard user can't. Once the Wikis are set to private, a standard user shouldn't be able to view a private Wiki. I've set up the scope below to implement this.

class Wiki < ActiveRecord::Base
  belongs_to :user
  validates :user, presence: true

  scope :visible_to, -> (user) do
     return all if user && (user.premium? || user.admin?)
     where(private: [false, nil])
  end
end

When I save the Wiki to private, it is still displaying when I switch to a standard user. Initially I thought the scope wasn't working correctly, however I checked the console and the private Wiki is displaying nil when it should be true. So I'm not sure why the Wiki isn't saving as private.

screen shot 2017-06-27 at 7 24 11 pm
bdougie commented 7 years ago

There is quite a bit missing from this question, can add a bit more, including a screenshot of the error and your best guess on what is going on. Consider adding the SCQA technique when crafting a question.

Situation - how did you get here? Complication - What is the problem? Question - What is your question? (Make this clear) Answer - What is your best guess at what is wrong and how it could be solved

I also highly recommend adding screenshots or error messages as well. All of the above makes it easier when getting general help from myself, Stack Overflow, and other channels. I hope you don't mind a little push back, but I want to make sure as we approach these types of problems that you get some value from my explanations as well

bdougie commented 7 years ago

So it looks as if you are not doing anything with the private checkbox in the :update method. We walked through a bit of this last session. You will need to add a line that updates current_user.private to true in database.

Use debugging techniques like raise to access the in browser rails debugger (the better_errors gem), I added. You can also look back to Bloccit for guidance as well. https://github.com/SageBeard/bloccit/blob/master/app/controllers/topics_controller.rb#L29-L41

Let me know if this makes sense

SageBeard commented 7 years ago

That's working. My next question is for my scope -- I created my user scope for the private wikis, and now I'm trying to add the scope into my Users show view. I'm not sure how to do this with devise. I have it added below in the user_sessions_controller, but that's not working.

class Wiki < ActiveRecord::Base
  belongs_to :user
  validates :user, presence: true

  scope :visible_to, -> (user) do
     return all if user && (user.premium? || user.admin?)
     where(private: [false, nil])
  end
end
class Users::SessionsController < Devise::SessionsController
  # before_action :configure_sign_in_params, only: [:create]
  def show
    @wikis = @user.wikis.visible_to(current_user.premium)
  end
end
bdougie commented 7 years ago

There are a few things wrong with this line

    @wikis = @user.wikis.visible_to(current_user.premium)

You are passing current_user.premium, which will return a boolean, but your scope expects a User object. Pass in just current_user there.

If you put this in the rails console, what does it return?

user = User.last
Wiki.all.visible_to(user)