mardi 21 avril 2015

change logo in layout depending on user

In my app I want to change the logo and other default pictures depending on which user is logged in. Anyone has a good tutorial or know how to do this? I have been looking for this a lot but there is no much information about it.

EDIT: I posted like that because I don't know much about this topic, sorry for the inconvenience. I have the user login system done and is possible to create new roles. Currently the roles dosen't do anything but it exist.

I'm now adding my main layout, user model and role model. What I want is to change the "wfront/logo" depending on the current user role. I know it should be simple but I'm very new at rails and this app was program by another person.

Please tell me if you need more info.

views/layouts/application.html.haml
//this is my app layout and I want to change the logo dynamically 'wfront/logo'
    %html
      %head
        %title= full_page_title
        = render 'wfront/head'
        = csrf_meta_tags
      %body{ class: "fixed-header fixed-navigationx smart-style-0 #{cookies[:menu_state]}" }
        %header#header
          = render 'wfront/logo'
          = render 'wfront/context_select'
          = render 'wfront/subcontext_select'
          = render 'wfront/toggle_menu_button'
          = render 'wfront/user_info'
          .pull-right
            = render 'wfront/currency_select'
            = render 'wfront/date_select'
        %aside#left-panel
          = render 'wfront/nav_menu'
          %span.minifyme{ data: { action: 'minifyMenu' } }
            %i.fa.fa-arrow-circle-left.hit

        #main
          #ribbon
          #content
            .row
              = render 'wfront/title'
              .col-xs-12.col-sm-5.col-md-5.col-lg-7
                %ul#sparks
                  = render 'wfront/sparks'
                  = render 'wfront/exchange_rates'
            = yield

        .page-footer
          = render 'wfront/footer'

        = render 'wfront/quaam'
        = javascript_include_tag 'application', "data-turbolinks-track" => true
        = yield :scripts

        = render 'wfront/modal'

--

  models/user.rb
    # == Schema Information
    #
    # Table name: users
    #
    #  id                     :integer          not null, primary key
    #  email                  :string(255)      default(""), not null
    #  encrypted_password     :string(255)      default(""), not null
    #  reset_password_token   :string(255)
    #  reset_password_sent_at :datetime
    #  remember_created_at    :datetime
    #  sign_in_count          :integer          default(0), not null
    #  current_sign_in_at     :datetime
    #  last_sign_in_at        :datetime
    #  current_sign_in_ip     :string(255)
    #  last_sign_in_ip        :string(255)
    #  created_at             :datetime
    #  updated_at             :datetime
    #  name                   :string(255)
    #

    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :registerable, :timeoutable and :omniauthable
      devise :database_authenticatable, :lockable,
             :recoverable, :rememberable, :trackable, :validatable

      has_many :account_users
      has_many :accounts, through: :account_users
      has_many :taxes, -> { uniq }, through: :accounts

      validates :name, :email, presence: true, uniqueness: true

      include Concerns::Roleize
      roleize

      def primary_account
        self.accounts.first
      end
    end

--

   model/role.rb
    # == Schema Information
    #
    # Table name: roles
    #
    #  id            :integer          not null, primary key
    #  name          :string(255)
    #  resource_id   :integer
    #  resource_type :string(255)
    #  created_at    :datetime
    #  updated_at    :datetime
    #

    class Role < ActiveRecord::Base
      belongs_to :resource, polymorphic: true
      before_create :tableize_name

      def tableize_name
        self.name = self.name
      end

      def self.method_missing(name, *args, &block)
        roles = self.where(["name =?", name])
        if roles.any?
          scope roles.first.name.to_sym, ->{roles.first}
          return self.send(name.to_sym)
        end
        super
      end
    end

Aucun commentaire:

Enregistrer un commentaire