I'm using the Garb gem to access Google Analytics data for my site. I've created the model that extends Garb::Model, but I can't find any information on Rails conventions for which directory should contain such a table-less model; is it just app/models/? or lib/? Is there a way I should mark this model to indicate that its table-less?
I'm sure it's too late now, but for future reference I've always just seen it put in app/models. It's still a model! ActiveRecord goodness is just an implementation detail.
Related
I'm new to the concept of test-driven development (and fairly new to Rails). I'm stuck with how to approach writing a test for my application.
As a backgrounder, the web app is a peer evaluation tool where individual groups are assigned surveys where they are asked to evaluate their peers. So a sample question would look like this:
Rate your peers on time management.
- Peer 1: __
- Peer 2: __
- Yourself: __
How I have this set up internally is as so:
I have a Survey model with a has_many association to an Assignment model (which controls the visibility of surveys among groups), which acts as a join model for Survey and a Group model. In the Group model there is a has_many association with Users.
Because multiple groups can be assigned the same survey, I have to find out which Group a certain user is asked to evaluate. (So the survey would be the same, but the people that show up as in the sample above would change from user to user).
I have decided to create a method, assignees_for_user(user), which would return, essentially, the user's group(s) that have been assigned to the survey.
I want to be able to unit test this method. I am using rspec and factory_girl so far. I have a few questions:
Is my approach even correct design-wise?
Should I test this? If so, I'll need to create instances for Assignment, Group, and User, right?
What's the best way I can stub/mock these instances without persisting them onto the database? Or will I have to?
Thanks!
This answer was more complicated than I expected (and to be honest, I guess that's why no one answered it :P)
You can use frameworks like mocha to mock things like find: How to test ActiveRecord witout Rails?
I ended up using rr instead. But when it comes to activerecord associations, I still haven't found a good way to do it because I don't fully understand the ActiveRecord association proxy objects. So I guess my takeaway in all of this is, honestly, not to waste my time mocking associations in the first place: As an old adage goes, you can't unit test something that isn't your own!
Can I suggest that refactoring the business logic from both the activerecord model (think of it as a database adaptor) and controller (think of it as a GUI adaptor) to plain old ruby objects (PORO) in the lib/ is the way to go.
Hexagonal Rails by Matt Wynne, Rails Conf 2013 Building Extractable Libraries in Rails by Patrick Robertson, Making ActiveRecord Models Thin and Gary Bernhardt are good places to start.
Why controllers are named "users_controller.rb" and models are not named "user_model.rb"?
Why there is "application_controller.rb" but inside views the folder "layout" is not named "application"?
Code flows from thought best when the naming supports the developers internal model of the problem. When building an application, I don't think of finding a user model (UserModel.find) I think of finding a user (User.find). On the other hand, the controllers are the translation layer between the web interface and the data store (and business logic), so it makes more sense to call them something different.
There's also the problem of namespacing; if both my model and controller are named User, then which User am I referring to at any given moment? In this case, either you name everything with their type, which runs into the problem I describe above, or one 'wins' and is allowed to be referenced 'bare'. It seems to make the most sense that the model would win, so as to provide a better mental mapping.
Inside app/views/layout is application.html.erb and you can have other layouts which are selected by different controllers.
In the end, however, these were choices made during the development of Rails, and they are entirely stylistic choices based on what the developers thought made the most sense, so there isn't really a 'right' answer to your question, unfortunately. In fact, some similar decisions have been revisited. (application_controller.rb used to just be named application.rb.)
Ruby on Rails follows "Convention over configuration" principle. Particularly naming conventions are extensively used by Rails while mapping your routes to controllers, auto-loading and reloading classes, finding appropriate template for an action and many other features.
That principle leads to some restrictions as you can't easily break some of the conventions without getting into troubles. But on the other hand it makes our lives easier as we get smaller amount of configuration and can easily move from one Rails project to the other because all of them have similar structure and follow same conventions. In addition, I believe, that makes Rails core development much easier as core team have a lot of information about how the project using Rails will be structured and they don't have to worry that much about generalization. They simply assume you play by the rules and follow conventions.
Though, I doubt many of naming conventions have serious reasoning behind them. I think at some point someone just decided that it'd be easier for Rails to handle your controllers and distinguish them from other classes if they all have Controller suffix. And here we are having all our controllers in app/controllers directory with that suffix.
I have an app that I'm building and would like to implement rails persistence to the backbone front end.
can someone point me to a simple repo or tutorial where i can learn about the configuration needed in rails to be able to persist data.
Thanks
I suggest to take a look at the backbone-rails gem.
It also provides a backbone:scaffold generator (I find that many rails generators can be used like "tutorial").
In Rails 2 I know of a few plugins for enumerations such as acts_as_enumeration and enumerate_by but they don't seem to be maintained or updated for Rails 3. Preferably, the solution would store the enum in memory rather than a database for performance reasons but really any method would be useful since it can always be cached.
I did find enumerated_attribute that claims to work with Rails 3 but quite honestly I don't like the API and was hoping for another good solution.
(Sorry for only linking to the one plugin but it won't let me post more than one link until I get a higher reputation)
I am currently using lwe's simple_enum which seems to be actively developed and stores values on memory or if you prefer on a table.
If you're using DataMapper as your ORM have a look at dm-types which includes an Enum type.
There is this gem, enumerate_it, it has good documentation and very well done!
I started creating a domain model and now I asking myself, how can I map this domain model to a NHibernate Data Model ((using Fluent NHibernate)? Is there anywhere a good and simple example of how to do that?
With Data Model I didn't think about the physical/relational Database Model(!) What I meant was the Data Model in the Data Access Layer. So maybe I should change the term into Data Access Layer Data Model?! I hope some of you can follow me^^
I'd start reading the NHibernate manual, it's not very long and in chapter 5 covers many details about the different options that exist in the mappings. I find also important chapter 6 about collection mapping an 8 about inheritance.
Maybe you can start with the getting started page from fluent nhibernate, but to understand all the options you will have to lookup their meaning in the NH manual as Fluent NHibernate assumes that you are already familiar with the XML.
I don't have enough information about NHibernate in Action but the good old Hibernate in Action was also useful because the mapping examples for the Java version are in most cases valid on .NET and provides examples and detailed information on each parameter.
This is the easiest one for getting started in a step-by-step manner that I have come across
Your very first NHibernate application – Part 1
However, I would recommend that you simply download the latest binaries from the Fluent NHibernate download area rather than getting the source from Subversion, installing Ruby and building it locally on your machine but that's up to you.
Well, have you tried www.nhforge.org and http://fluentnhibernate.org/ ? Both have excellent introductory guides.
Take a look at the Getting started page on the Fluent NHibernate site. And definitely take a look at the Auto Mappings capabilities. I was up and running in a couple of hours. Good luck!