I am following a RailsCast on implementing Facebook API. In the User model, I have a method:
def facebook
#facebook ||= Koala::Facebook::API.new(oauth_token)
which creates an instant variable that can be shared with other methods within User model.
My question is:
If my Rails app has 100 users online, each of them create one instance #facebook, how does Rails know which #facebook is associated with with user?
Instance variables are associated with the instance of the object they belong to.
Users interact with their own User instance because it's associated with their session or a specific request. Requests are processed using request-specific instances of controllers that in turn deal with a specific instance of a User, hence #facebook value.
It doesn't. There is no such thing as "online" in a web app, every time you click a link, a request a sent. For that request, all classes are instantiated, including running the code you posted.
After the request is completed, those classes are disposed of.
Related
I'm creating an automation framework. The test cases are separated from the pages that they interact with. I'm also dealing with multiple different data objects. As the framework has gotten more complex, we realized that we have to determine the best way to read this data, add to it, change it, reset it, etc...
Example:
You can have multiple User objects.
A User object can have multiple Group objects, single Email field.
A Group object can have a single Name field, multiple Invited User objects, Member User objects. -An Invited User and Member User are User objects, but can not have their own groups-
So, if a User creates a Group, invites multiple Users, and some of those Invited Users convert to Member Users... would we:
A) Have the test case pass in a User, Group, and Invited User objects to a page class that creates it. Then the page class adds the Invited User object to the Group object, and the Group object to the User object. Then the test case only needs to pass around the User object when it wants to convert some of those Invited Users to Member Users and eventually do cleanup after the test is done.
B) Have the test case pass in a User, Group, and Invited User object to a page class that creates it. Then the test case would pass in a Group object and Invited User object to a page class to convert them into Member Users. Then the test case would need to keep those relationships straight when moving through the test and doing cleanup.
C) Have the test case create the objects, but only pass specific fields needed for the operation of the method it is calling. Create Group would need Name. Invite User would need Email, etc... The test case would then need to update the objects and keep the relationship between them (as either a single object, or multiple objects).
Last thing... If C is the best option, at what point do you stop passing in specific fields of the object and just provide the object as a whole?
Each test can be decomposed into a number of steps. Some steps automate the web browser. Others insert data to the database, or query the database in order to make assertions. The challenge is to organize these "steps" as methods in one or more classes. An individual test should just call these methods.
The internals for these methods can use selenium page objects, repositories, entity objects — whatever is necessary to perform the test. A sample test could look something like this (you didn't specify a programming language, so I used C#):
Given.User.Create("Johnny", "Testerson"); // insert user into database
Given.User.LogIn("Johnny", "Testerson"); // use page object to log in
// Use page objects to create group
When.Group.Create(name: "Test group",
invites: new [] { "a#example.com", "b#example.com" });
// Query database, make assertions
Then.Group.InvitesShouldBeSent(group: "Test group",
recipients: new [] { "a#example.com", "b#example.com" });
I simply need the name of the currently logged in user. The same that gets displayed in gsp with <sec:username/>. I'm at a loss as to what to do.
Here's previous answers and questions - they all seem to require some import and none of the solutions works for me:
Grails Spring Security (get current user)
How to get id of the current user logged in grails?
How to get current_user by using Spring Security Grails plugin in GSP
Grails and Spring Security: How do I get the authenticated user from within a controller?
Specifically, I want to access the username insides a controller and put it into a variable of a domain instance in order to persist it (alongsides other information) into the database for logging purposes. Insides that controller, there's nothing to be seen that even remotely refers to Person/User classes, spring security or any other thing that sounds like it might do what I intend.
You can access the domain object corresponding to the logged-in user using springSecurityService.currentUser, and then fetch the username (or whichever other properties you require) from there. Due to Groovy's dynamic nature you don't need to import the domain class or the SpringSecurityService class to do this, simply using an untyped dependency injection
def springSecurityService
and you can access springSecurityService.currentUser?.username (or whatever).
Though as Burt points out in a comment, if it's only the username you want then it is more efficient to use springSecurityService.authentication.principal.username as this does not need to load the user object from the database. You only need currentUser if you want other properties of the user object aside from the ID or username.
Just figured it out: In the controller, def springSecurityService comes just after the opening bracket of the controller class (that's class YourController {) and the variable assignment looks like this: def username = springSecurityService.authentication.principal.getUsername() - apparently playing around with some code posted here has helped me.
I'm relatively new to rails and am using devise for my user sign up and sign in processes. At sign up if a user doesn't have an invite token I would like them to also sign up and create their business that will be associated with their user account.
Below is a screenshot of the error I am getting in my RegistrationsController when trying to create the new business.
Here is my code in the registrations_controller.rb:
if params[:invite_code]
...
else
resource = build_resource({})
resource.businesses.build() # Inserts a blank object for business
respond_with resource
end
Any ideas on why it isn't able to pass the business information provided and create a new business? Thanks in advance for any help.
I think it's just a pluralization issue. The has_one association on the User should be singular (:business) as should the method call in the controller (resource.business.build()) and accepts_nested_attributes_for.
That being said, that part of the controller should never even be getting hit. All of your logic should be contained within this first conditional. Everything happening here should probably be pulled out into another method and called after the successful save. Here's a new gist with a refactoring of registrations_controller.rb: https://gist.github.com/ccschmitz/7ea0a41180e25de9168d
I am using Deadbolt 2 with Playframework 2.1.
In the getSubject() function of my DeadboltHandler I check the user password and retrieve the user from the database.
Is it possible to access this user in my controllers to avoid retrieving the user twice per request?
Steve, the developer of Deadbolt, suggested me the following:
the trick is to store the user in the context, and then have your controller or deadbolt handler to access it. This allows you to store the user by the actual class, and not have to use getSubject() and cast the resulting Subject to your actual User class.
So I decided to save the User object in the args-Field of Http.Context.current() and it works like a charm!
I am trying to make a REST API with CakePHP but I'm not sure how to approach resources that are more than one level down. For example, I want to make my API so that when a client sends a get request to /users/1/friends.json, a JSON representation of that user's friends is returned. What would be the preferred way to approach this? Should I create a Friendships controller and model and reroute as necessary or perhaps keep everything in the users controller? Just want to know what the standard way of doing this is.
If you want to expose a lot of features like: creating/updating/deleting a user, getting a user, getting all users, etc. you end with a lot of methods in your controller.
I would suggest to use different controllers:
a UserController which owns CRUD methods related to a user, and a method to fetch all users. That would mean 5 methods in this controller;
a FriendController which owns all methods related to a friendship relation: getting user's friends but also creating/removing relations between two users.
I don't know CakePHP but I wrote something about Symfony2 REST APIs and my use case was a User API. You could pick ideas like how to manage friendship relations between users.