Skip to content

Application

amonfog edited this page Jun 10, 2013 · 5 revisions

In this part of the documentation we will be looking at the basic code of the website. Everything that is used on every page or accessible by every method, model, etc. is posted here.

##Application Helper The application Helper is filled with pre-set variables that are accessible on every page. In this case the html title and <h1> tag are defined here.

###Render_title

def render_title
  return @title if defined?(@title)
  "Inspiratieplanet"
end

This small method renders the title on every page. if a title is defined it will use that title. If not it will use the default name "Inspiratieplanet". the title is called in the application.html.erb.

###Render_subtitle

def render_subtitle
  return @subtitle if defined?(@subtitle)
  " "
end

This small method renders the title in the <h1> tag on every page. If a title is defined it will use that. If not it will put a space-bar in it's place (creating an empty <h1> field) Of course this code could be put on the page, but this technique allows the page text to begin on the same place in every page plus it prevents collapsing of a few elements.'

##Application controller

The application only has 1 string: `protect_from_forgery'. this small row is part of the Rails platform. Each form get's a hidden field with a random set of numbers and letters. the form send to the user must have the same set of numbers and letters as the send form. This protects the site from cross-site request forgery.

##Application Views The application has 1 view every page contains the content from this view. (except for the pages in Public. Let's breakdown this document and pick the most important parts.

###Header

<title><%= render_title %></title>

The title rendered from the application helper It is set in the diffrent methods troughout the website.

<%= stylesheet_link_tag    "application", :media => "all" %>

The stylesheets are called from the server. In developement they are just all entered here, in deploy all css is cached and compiled to 1 document and linked here.

<%= javascript_include_tag "application" %>

The javascripts are called from the server. In developement they are just all entered here, in deploy all javascript is cached and compiled to 1 document and linked here.

<%= csrf_meta_tags %>

metatags are put here. the metatag area can be populated by diffrent Gems (that are not used in our app)

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />

This meta is put here to disable re-sizing the page on mobile devices. This is necessary to make the page responsive.

###Content ####head-content

<% if user_signed_in? %>
  <%= current_user.name %>
  <%= link_to('uitloggen', destroy_user_session_path, :method => :delete) %>
  <% if current_user.admin? %>
<li class="gebuiker"><a href="/dashboard/admin/">Admin</a></li>
  <% end %>
<% else %>
  <%= link_to('inloggen', new_user_session_path)  %>
<% end %>

This feature checks if the user is signed in and then puts it's name in the banner of the site If the logged-in user is an admin a link to the admin-dashboard is shown. There's also a logout button shown for each type of logged-in user. When no user is logged in only the login button is shown.

####Page content

<h1><%= render_subtitle %></h1>

The subtitle is rendered from the current method in the controller. render_subtitle is defined in the application helper.

<% if flash[:error] %>
	<div id="error"><%= flash[:error] %></div>
<% end %>

<% if flash[:alert] %>
	<div id="alert"><%= flash[:alert] %></div>
<% end %>

<% if flash[:notice] %>
<div id="noticed"><%= flash[:notice] %></div>
<% end %>

Flash[:error],[:alert],[:notice] are only shown when an error, alert or notice occured. The div does not exist and thus doesn't needs to be in a collapse state if no error exists.

<div id="wrapper">
	<%= yield %>
</div>

Yield calls the html.erb for the current controller#method. all content put in for example index.html.erb of articles will be "glued" in place when the user requests /articles/

##Environments Some changes have been made to the environments to allow specific functions to run on the servers.

###Development Environment

# Define host for mailer
config.action_mailer.default_url_options = { :host => "localhost:1030" }
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1030 }
config.action_mailer.delivery_method = :smtp

These settings allow mails to be send from the server (to reset passwords in the Devise controller). They are set to be intercepted by Mailcatcher.

##Database.yml

The database settings file has some extra options to allow deployment in capistrano

production:
  adapter: postgresql
  encoding: unicode
  database: inspiratieplanet
  pool: 5
  username: inspiratieplanet
  password: inspiratieplanet

Basically we are seeing some usernames and passwords here. (don't try to get them to work, we took our freedom to change usernames and passwords on the server).

##Deployment.rb

Deployment options are put in a special file in the config folder. the settings you see bellow are the settings that put our server online in the first place.

require 'bundler/capistrano'
load 'deploy/assets'
 	
set :application, "inspiratieplanet"
set :ip_address, "149.210.131.191"
 
set :deploy_to, "/home/deploy/#{application}"
set :user, 'deploy'
set :use_sudo, false
 
set :scm, :git
set :repository, "git@github.com:amonfog/inspiratieplanet.git"
set :branch, "master"
set :repository_cache, "git_cache"
set :deploy_via, :remote_cache
set :ssh_options, { :forward_agent => true }
 
default_run_options[:pty] = true
 
role :web, ip_address
role :app, ip_address
role :db,  ip_address, :primary => true

desc "tail log files"
task :tail, :roles => :app do
  run "tail -f #{shared_path}/log/#{rails_env}.log" do |channel, stream, data|
    puts "#{channel[:host]}: #{data}"
    break if stream == :err
  end
end

desc "populate database"
task :bootstrap do
	run("cd #{deploy_to}/current && /usr/bin/env rake bootstrap:all RAILS_ENV=production")
end

desc "populate database with admin"
task :addadmin do
	run("cd #{deploy_to}/current && /usr/bin/env rake addadmins:all RAILS_ENV=production")
end

The first part of the code defines the usernames, passwords and settings for Capistrano (a Deploy gem). The second part of the code consists of Tasks. On the developers side of the site developers can simply type cap bootstrap:all in their consoles to populate the database with categories and `cap addadmin:all' to add an admin account to the database on the server side.

##Routes

1 route is added to define the site's homepage root to: 'articles#index' In this case the articles#index is the root of the website.

##Gems

Here is a list off all the gems used in this project:

source 'https://rubygems.org'

gem 'rails', '3.2.12'
gem 'bootstrap-sass', '2.1'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.0.1'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
gem 'jquery-rails', '2.0.2'
gem 'devise'
gem 'capistrano'
gem 'sass-rails',   '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.11.0'
  gem 'guard-rspec', '1.2.1'
  gem 'guard-spork', '1.2.0'  
  gem 'spork', '0.9.2'
  gem 'rails_best_practices'
end

# Gems used only for assets and not required
# in production environments by default.

group :test do
  gem 'capybara', '1.1.2'
  gem 'factory_girl_rails', '4.1.0'
  gem 'cucumber-rails', '1.2.1', :require => false
  gem 'database_cleaner', '0.7.0'
  # gem 'launchy', '2.1.0'
  # gem 'rb-fsevent', '0.9.1', :require => false
  # gem 'growl', '1.0.3'
end

group :production do
  gem 'pg', '0.12.2'
end

# image management
gem 'carrierwave'
gem 'mini_magick'
# ftp storage of images
gem 'carrierwave-ftp'
Clone this wiki locally