webdevRefinery Forum: Always execute code - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline Mo3 

  • Brogrammer
  • Group: Moderators
  • Posts: 1950
  • Joined: 21-July 10
  • LocationStuttgart, Germany
  • Expertise:PHP,Java,Javascript,Python,Ruby on Rails,Node.js

Posted 08 November 2011 - 09:50 AM (#1)

Always execute code


This is really annoying me.

I have a couple of tasks that I have to execute every time any request takes place. (Checking if an user is logged in, checking if there are any new messages, etc)

I tried putting the relevant code into application_controller.rb and using a before_filter, but the variables wouldn't be accessible in the view.

I ended up putting that code into every single controller action, but there has to be a nicer way, no?

Quote

A person who thinks all the time has nothing to think about except thoughts. So he loses touch with reality, and lives in a world of illusion called the past. Things are not explained by the past, they are explained by what happens right now. That creates the past, and it begins here. That's the birth of responsibility.
0


User is offline _Sam 

  • Beep Beep
  • Group: Members
  • Posts: 633
  • Joined: 11-March 10
  • LocationZurich, Switzerland
  • Expertise:HTML,CSS

Posted 08 November 2011 - 10:03 AM (#2)

Can't you just put this Code into another file and include this every time? That way you have the Code only once present and if it changes for some reason you only have to do it on one place.
(ಠ_ಠ)
0


User is offline NoizeMe 

  • Group: Members
  • Posts: 591
  • Joined: 06-May 10
  • LocationGermany
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Node.js,SQL,MongoDB,CouchDB,Cassandra

Posted 08 November 2011 - 10:05 AM (#3)

View PostMo3, on 08 November 2011 - 09:50 AM, said:

This is really annoying me.

I have a couple of tasks that I have to execute every time any request takes place. (Checking if an user is logged in, checking if there are any new messages, etc)

I tried putting the relevant code into application_controller.rb and using a before_filter, but the variables wouldn't be accessible in the view.

I ended up putting that code into every single controller action, but there has to be a nicer way, no?


This screams out for a bootstrapper :).
That's what I'm doing when I need to do some tasks.
Posted Image
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 November 2011 - 10:35 AM (#4)

helper_method :current_user

etc, you should be using lazy loads, ex:
def current_user
  @user ||= User.find_by_id(session[:user_id])
end

Reserved.
0


User is offline Mo3 

  • Brogrammer
  • Group: Moderators
  • Posts: 1950
  • Joined: 21-July 10
  • LocationStuttgart, Germany
  • Expertise:PHP,Java,Javascript,Python,Ruby on Rails,Node.js

Posted 08 November 2011 - 10:39 AM (#5)

View PostThatRailsGuy, on 08 November 2011 - 10:35 AM, said:

helper_method :current_user

etc, you should be using lazy loads, ex:
def current_user
  @user ||= User.find_by_id(session[:user_id])
end



That's exactly what I do, but @user isn't accessible in the view. (actually, its empty)

Quote

A person who thinks all the time has nothing to think about except thoughts. So he loses touch with reality, and lives in a world of illusion called the past. Things are not explained by the past, they are explained by what happens right now. That creates the past, and it begins here. That's the birth of responsibility.
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 November 2011 - 12:36 PM (#6)

View PostMo3, on 08 November 2011 - 10:39 AM, said:

That's exactly what I do, but @user isn't accessible in the view. (actually, its empty)

Yes, @user isn't, which is why you use the helper_method ex:
class ApplicationController << ActionController::Base
  helper_method :current_user

  private
  def current_user
    @user ||= User.find_by_id(session[:user_id])
  end
end

Hello <%= current_user.username %>.

Reserved.
0


User is offline Mo3 

  • Brogrammer
  • Group: Moderators
  • Posts: 1950
  • Joined: 21-July 10
  • LocationStuttgart, Germany
  • Expertise:PHP,Java,Javascript,Python,Ruby on Rails,Node.js

Posted 08 November 2011 - 12:37 PM (#7)

View PostThatRailsGuy, on 08 November 2011 - 12:36 PM, said:

Yes, @user isn't, which is why you use the helper_method ex:
class ApplicationController << ActionController::Base
  helper_method :current_user

  private
  def current_user
    @user ||= User.find_by_id(session[:user_id])
  end
end

Hello <%= current_user.username %>.



But that calls to the database every time I use current_user in the view.. which is 4-5 times D:

Quote

A person who thinks all the time has nothing to think about except thoughts. So he loses touch with reality, and lives in a world of illusion called the past. Things are not explained by the past, they are explained by what happens right now. That creates the past, and it begins here. That's the birth of responsibility.
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 November 2011 - 02:31 PM (#8)

View PostMo3, on 08 November 2011 - 12:37 PM, said:

But that calls to the database every time I use current_user in the view.. which is 4-5 times D:

No, see the ||= it only assigns if nil, but also probably want something like:
if session[:user_id].present?
  @user ||= ...
end

Reserved.
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services