GithubHelp home page GithubHelp logo

Comments (12)

tute avatar tute commented on August 18, 2024

I think it might be because you use u.user.created_at on the second grant_on call, but it should be u.created_at. Please try it!

You could name the block argument user to make that clearer:

    def initialize
      # New user
      grant_on 'users/registrations#create', badge: 'New-Baker', model_name: 'User'

      # 1 month user
      grant_on 'users/sessions#create', badge: "Typical-Baker", model_name: 'User', to: :itself do |user|
        user.created_at < "2022-04-25 00:00:00" && user.created_at > "2022-03-01 00:00:00"
      end

      # 1 year user
      grant_on 'users/sessions#create', badge: "Eternal-Baker", model_name: 'User', to: :user do |user|
        user.created_at < "2021-05-25 00:00:00"
      end
    end

from merit.

tute avatar tute commented on August 18, 2024

It could also be that created_at returns a Time-like object, whereas "2022-04-25 00:00:00" is a String.

If you are on Rails you could use syntax like 1.month.ago to use comparable data types.

from merit.

pixelRena avatar pixelRena commented on August 18, 2024

Thanks for the response!

I changed up my code quite a bit

routes.rb

devise_for :users, controllers: { registrations: 'registrations', sessions: 'sessions' }

badge_rules.rb

# New user
      grant_on 'registrations#create', badge: 'New-Baker', model_name: 'User'

      # 1 month user
      grant_on 'sessions#create', badge: "Typical-Baker", model_name: 'User', to: :user do |user|
        user.created_at < 1.month.ago && user.created_at > 1.year.ago
      end

      # 1 year user
      grant_on 'sessions#create', badge: "Eternal-Baker", model_name: 'User', to: :user do |user|
        user.created_at < 1.year.ago
      end

Sessions Controller

class SessionsController < Devise::SessionsController
    def create
        @user = resource # Needed for Merit
        super
    end
  end

Although that did not change or fix anything.
When I log in, I receive this message still [merit] no target_obj found on Rule#applies?.
Although it works with any other controller like users#show, but I would not prefer a profile to be triggered in order to get a badge. It still seems weird that when logging in, it does not work even when overriding devise sessions itself.
which is custom made to show profiles of that specified user inside of a user controller I made, here is what it looks like:

users_controller.rb

class UsersController < ApplicationController
    before_action :authenticate_user!, except: [:show, :index, :followings, :followers]
    def show
            @user = User.find_by_username(params[:username])
            render :show
        end
end

from merit.

tute avatar tute commented on August 18, 2024

It's looking better! Now it may be because you don't need to specify , to: :user in the last two cases. You already get the user in the block, and adding that option is akin to calling it twice (user.user). Try that.

I didn't see that yesterday to correct it in my snippet.

from merit.

pixelRena avatar pixelRena commented on August 18, 2024

I removed the to: :user portion but it is still giving me [merit] no target_obj found on Rule#applies?

I removed the to portion:

# New user
      grant_on 'registrations#create', badge: 'New-Baker', model_name: 'User'

      # 1 month user
      grant_on 'sessions#create', badge: "Typical-Baker", model_name: 'User do |user|
        user.created_at < 1.month.ago && user.created_at > 1.year.ago
      end

      # 1 year user
      grant_on 'sessions#create', badge: "Eternal-Baker", model_name: 'User'  do |user|
        user.created_at < 1.year.ago
      end

from merit.

pixelRena avatar pixelRena commented on August 18, 2024

I also did more testing.
I called the model_name: 'Resource' and if I put for example resource.present? it will give me the badge after logging in. So I'm thinking something with the controller or model is an issue?
I cant call resource.created_at since that model does not have that attribute.

from merit.

tute avatar tute commented on August 18, 2024

Can I see your whole code somewhere in GitHub so I can further debug? I need to be able to replicate your issue.

from merit.

pixelRena avatar pixelRena commented on August 18, 2024

Yes! I've sent you a invite as it is a private repo.

from merit.

pixelRena avatar pixelRena commented on August 18, 2024

The badges are also meant to display on the profile, so once logging in you can also check the profile for badges or the console after logging for details.

from merit.

tute avatar tute commented on August 18, 2024

I tried debugging but I couldn't make it work in time. This may be a bug in merit.

We'd want to grant to: :action_user I think, and then we don't need the model_name: attribute, but ran out of time.

For now, please use add_badge manually in the controller. Thanks for reporting.

from merit.

pixelRena avatar pixelRena commented on August 18, 2024

Okay, no problem. Thanks for attempting! Let me know if any fix for this
Closing issue now.

from merit.

chandranshu12 avatar chandranshu12 commented on August 18, 2024

Hi @serenuy ! We wanted to try out merit in our application. So, was just going through the issues list to see what I should be aware of. Yours was the first one.

For devise sessions controller, the resource is actually a session object and not the user object. users is just a namespace here. Once the session is created, it'll store :user_id in the sessions_hash which you can use to find out your user and see if it works.

from merit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.