Thursday, October 8, 2009

Adding validation to views

This is in continuation to my previous post. I had the view ready. the path of the view was of the form dev/%/upload.

This would mean the page view could be generated with URLs of the form -
http:///dev/Sony/upload

assuming that Sony is the company for which you want to find the uploads.

Now, we need to validate that the user who is generating this URL actually belongs to Sony.

I added those validations using PHP code snippet by clicking on arguments link as shown -


Next, go to the validation section, select the validator as PHP and enter -

global $user;
profile_load_profile($user);
return $argument == $user->profile_company;

Refer image -
You can also choose the behavior you want in case of default arguments, illegal arguments, etc

Save the view. Check the preview by giving some valid and invalid arguments.


We are all set with a foolproof view definition.



Thursday, October 1, 2009

Building views in Drupal with Profiles and Filefield cck

I have a requirement where a user has some profile fields like company name. I have some content which are papers uploaded by every user.
I need to build a view so that a user can see technical papers of all the people who work for the same company.


This means implementing views to filter on the dynamic value of a profile attribute. There are filters on Current user, user status etc. I wanted to filter on the
user attribute of company.

What I have with me -
I have implemented the following -
1. Enabled profile module
2. Added 2 fields to the user profile -

* profile_job_title - which is an autocomplete textfield
* profile_company - a select list pre populated by site admin which contains the names of the company eg - Microsoft, Google, Yahoo, etc

3. Users are created by the site admin and he assigns them the property of the company to which they belong.
4. Created a new content type 'file' which is a filefiled cck field on a node
5. When these users log in to the site they can upload their files/ technical papers.

Requirement -
I wanted a view which said Uploaded papers of your company. This page would allow the logged in user to see all papers that are submitted by users
belonging to the same company



How I achieved this -

1. I decided to pass the company name dynamically as a parameter to the view.

2. I created a new view of the type node from Views > Add -


















3. I selected formatting options like table type, fields to be displayed, etc





4. I defined a path dev/%/upload where % would be the logged in user's company name which I will be passed as an argument to the view


5. Passed the argument as dev/%/upload Hence declare that profile company would be passed as an argument

6. I defined the filter for 'Profle Company field' and selected the Override option




7. Also selected other filter on 'node type' to be file


8. Click the Override option so that the value of this filter is picked from the argument and overrides any default.


9. While doing this, you can continue watching the Live preview on the same page towards the bottom of the configuration form





10. Finally select 'Page' as Display and enter an argument for the Company field. In my eg, I have added EA Sports as an argument. Click Preview. What you see is all files uploaded by users whose Company name is EA Sports. The company field shows the company name as well.



Hope this helps ! In the next post I will show how to ensure security and add validations to the view.

Back to Drupal 6

After a short stint with Ruby on Rails, I have moved to working on Drupal again. I am working for a telecom company who have different intranet applications and a fw external marketing sites.

We are evaluating whether Drupal would be a good option to use instead of some very expensive CMS tools that they are using. So I am creating some cool Web 2.0 feature rich interactive sites.
I see that Drupal is the right answer for all their woes with these CMS.

We are also evaluating some open source search alternatives for enterprise search - Solr, etc.

Keep going Team Drupal !

Wednesday, May 20, 2009

BDD with cucumber, rspec, ruby on rails - 1st in the series - Post 1

In my latest project, I am trying my hand at BDD. I would like to write about how to get started with BDD and write test first then code along the way -
Also, I am working in Agile SCRUM methodology which allows us to talk in terms of stories, tasks, impediments etc

1. I am building a social networking site based on internal data of the organisation - similar to facebookOkay - we have a story as -
Feature: As a user, I should be able to see [first name last name] on my profile
Scenario: As a user, I should be able to see [first name last name] on my profile
Given Manasi Vora is a user
And Manasi logs in to the site
Then Manasi should see Welcome Manasi VoraAnd Manasi should see Name: Manasi Vora
I created a view_person_profile.feature file to list the above and other scenarios.

2. I wrote a view_person_profile_steps.rb file for each of the above Given, When and Then statements
Given /^(.*) (.*) is a user in Profiles$/ do fname, lname
# I don't know what to write here ?? so I need a model to store the user
end

3. I now need a model to store the information of all users, which I created as -
ruby script/generate model Person

Added the required columns to the migration -
first_name
last_name
.
.
.
.

Ran the migration with rake db:migrate

Time for some specs now.. I wrote specs to test if the model is created and returns the first_name and last_name

4. I can now go back and complete my step in the steps.rb file -
Given /^(.*) (.*) is a user in Profiles$/ do fname, lname
@user[fname] = Person.find_by_first_name_and_last_name(fname,lname)
end
5. Next step to be implemented is about allowing the user to login
Continued further in Post 2...

Tuesday, May 19, 2009

has_many :through on the same model

1. I have 2 models person and secretary:

  • Person stores the details like, name, id, position, birthdate, etc
  • Secretary stores the mapping of person_id and secretary_id which are both foreign keys on person_id from person

class Person < ActiveRecord::Base
has_many :secretaries, :foreign_key => 'person_id'

has_one :assistant, :through => :secretaries, :conditions => [ on the attributes of secretary model]

...other details

end


class Secretary <>

belongs_to :person, :foreign_key => 'person_id'

belongs_to :assistant, :class_name => 'Person', :foreign_key => 'secretary_id'

. . . other details

end

2. Now I want to establish the association between Person to self as - A person would have a scretary who in turn is a person. Hence I have defined a new model assistant for the class Person and identified it in Secretary as -

belongs_to :assistant, :class_name => 'Person', :foreign_key => 'secretary_id'

3. Defining has_many :through / has_one :through - The most important to note here is to write - has_many :secretaries, :foreign_key => 'person_id'

and then

has_one :assistant, :through => :secretaries, :conditions => [ on the attributes of secretary model]

In my case has_one suffices since after the conditions only 1 active assistant would be returned. You cacn use has_many or has_one here.

4. Declare belongs_to :person, :foreign_key => 'person_id' in the Person model


And there you go !!! You are all set to enjoy the benefits of rails -Access the assistant as below -@person.assistant - Returns the assistant as a person object

@person.name - Person's name

@person.assistant.name - His asssitant's name
And a lot of fun ....

Friday, April 17, 2009

I love phpmyadmin

for its -
simplicity
wonderful UI
lightweight (runs in a browser without eating up all the memory)
features like extended inserts, single inserts to export the result of any query in the form of insert statements
export in all formats like csv, sql, xls

Thursday, April 16, 2009

handling mysql bigint from ruby

http://rubyforge.org/tracker/index.php?func=detail&aid=24387&group_id=4550&atid=17562