Skip to content

Commit

Permalink
Ability to Create/Update/Delete a campaign
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Forestier committed Aug 17, 2015
1 parent cae4f40 commit 3317985
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/assets/javascripts/corm_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ $(document).ready(function() {
});
$(document.getElementById('filter_begin')).datepicker(lang);
$(document.getElementById('filter_end')).datepicker(lang);
//$(document.getElementById('tache_term')).datepicker(lang);
$(document.getElementsByClassName('event_date')).datepicker(lang);
$(document.getElementsByClassName('filter_date')).datepicker(lang);
$(document.getElementById('opportunity_term')).datepicker(lang);
Expand All @@ -161,6 +160,8 @@ $(document).ready(function() {
$(document.getElementById('contract_date_begin')).datepicker(lang);
$(document.getElementById('contract_date_end')).datepicker(lang);
$(document.getElementById('contract_date_initial')).datepicker(lang);
$(document.getElementById('campaign_date_begin')).datepicker(lang);
$(document.getElementById('campaign_date_end')).datepicker(lang);

}
if ($.validator) {
Expand Down
20 changes: 15 additions & 5 deletions app/assets/stylesheets/bootstrap_overrides.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -156,35 +156,45 @@ h6 {

#event_date_begin_1i,
#event_date_end_1i,
#task_term_1i
#task_term_1i,
#campaign_date_begin_1i,
#campaign_date_end_1i
{
width:5em;
}

#event_date_begin_2i,
#event_date_end_2i,
#task_term_2i
#task_term_2i,
#campaign_date_begin_2i,
#campaign_date_end_2i
{
width:8em;
}

#event_date_begin_3i,
#event_date_end_3i,
#task_term_3i
#task_term_3i,
#campaign_date_begin_3i,
#campaign_date_end_3i
{
width:4em;
}

#event_date_begin_4i,
#event_date_end_4i,
#task_term_4i
#task_term_4i,
#campaign_date_begin_4i,
#campaign_date_end_4i
{
width:4.5em;
}

#event_date_begin_5i,
#event_date_end_5i,
#task_term_5i
#task_term_5i,
#campaign_date_begin_5i,
#campaign_date_end_5i
{
width:4.5em;
}
Expand Down
91 changes: 91 additions & 0 deletions app/controllers/campaigns_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# encoding: utf-8

##
# The Controller that manage Campaigns
#
class CampaignsController < ApplicationController
load_and_authorize_resource

##
# Display the list of all Campaigns by paginate_by
#
# GET /campaigns
def index
@campaigns = Campaign.order('date_begin DESC').page(params[:page])

respond_to do |format|
format.html # index.html.erb
end
end

##
# Render a page to create new Campaign
#
def new
@campaign = Campaign.new

respond_to do |format|
format.html # new.html.erb
end
end

##
# Process to insert a new Campaign into the DataBase
#
def create
@campaign = Campaign.new(params[:campaign])
@campaign.created_by = current_user.id

respond_to do |format|
if @campaign.save
format.html { redirect_to campaigns_path, :notice => t('app.message.notice.created_campaign') }
else
format.html { render :action => "new" }
end
end
end


def edit
@campaign = Campaign.find(params[:id])
end

##
# Process that udpate an existing Campaign
#
def update
@campaign = Campaign.find(params[:id])
@campaign.modified_by = current_user.id

respond_to do |format|
if @campaign.update_attributes(params[:campaign])
format.html { redirect_to(campaigns_url, :notice => t('app.message.notice.updated_campaign')) }
else
flash[:error] = t('app.save_undefined_error')
format.html { render :action => "edit" }
end
end
end

##
# Process that remove a Campaign from the DB
#
def destroy
@campaign = Campaign.find(params[:id])
respond_to do |format|
begin
if @campaign.destroy
format.html { redirect_to(campaigns_url, :notice => t('app.message.notice.deleted_campaign')) }
else
flash[:error] = t('app.delete_undefined_error')
format.html { render :action => "edit" }
end
rescue ActiveRecord::DeleteRestrictionError
@campaign.errors.add(:base, t('errors.messages.restrict_dependent_destroy', count: 1))
ensure
format.html { render :action => "edit" }
end
end
end

end
6 changes: 6 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def initialize(user)
cannot :manage, [Import, ImportAccount, ImportContact]
can :read, PaymentMode
can :read, PaymentTerm
can :read, Campaign
can :create, Campaign
can :update, Campaign, :created_by => user.id
elsif user.has_role? :user
# Role User
can :manage, Task
Expand All @@ -77,6 +80,7 @@ def initialize(user)
can :manage, Quotation
can :manage, QuotationLine
can :manage, Document
can :manage, Campaign
can :read, User
can :read, QuotationTemplate
can :read, Tag
Expand Down Expand Up @@ -128,6 +132,7 @@ def initialize(user)
can :manage, [Import, ImportAccount, ImportContact]
can :manage, PaymentMode
can :manage, PaymentTerm
can :manage, Campaign

else
# Role User by default
Expand All @@ -150,6 +155,7 @@ def initialize(user)
can :manage, Quotation
can :manage, QuotationLine
can :manage, Document
can :manage, Campaign
can :read, User
can :read, QuotationTemplate
can :read, Tag
Expand Down
4 changes: 1 addition & 3 deletions app/models/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class Campaign < ActiveRecord::Base

resourcify

validates :name, uniqueness: true

has_many :campaign_lines
has_many :campaign_lines, :dependent => :destroy
belongs_to :event_type
belongs_to :author_user, :foreign_key => 'created_by', :class_name => 'User'
belongs_to :editor_user, :foreign_key => 'modified_by', :class_name => 'User'
Expand Down
60 changes: 60 additions & 0 deletions app/views/campaigns/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<%= form_for(@campaign, :html => { :class => "well "}) do |f| %>
<% if @campaign.errors.any? %>
<div id="error_explanation" class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<h4><%= t('app.message.error.occured_error') %></h4>
<ul>
<% @campaign.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<div class="span2"><%= f.label :name %> :</div>
<%= f.text_field :name,{ :class => 'required'} %><span class="req" />
</div>

<div class="field">
<div class="span2"><%= f.label :date_begin %> :</div>
<%= f.text_field :date_begin, :value => @campaign.date_begin.strftime(t('date.formats.default')) unless @campaign.date_begin.blank? %>
<%= f.time_select :date_begin %>
</div>

<div class="field">
<div class="span2"><%= f.label :date_end %> :</div>
<%= f.text_field :date_end, :value => @campaign.date_end.strftime(t('date.formats.default')) unless @campaign.date_end.blank? %>
<%= f.time_select :date_end %>
</div>

<div class="field">
<div class="span2"><%= f.label :event_type %> :</div> <%= f.collection_select :event_type_id, EventType.order('label ASC, direction ASC').all, :id, :full_type, :include_blank => true %>
</div>

<div class="field">
<div class="span2"><%= f.label :notes %> :</div>
<%=f.text_area :notes , :display_with => :simple_format, :rows => "5", :style => 'width: 50%;' %>
</div>

<br />

<div class="field">
<% if !@campaign.id.nil? %>
<div class="span9"> <%= t("app.actions.created_by")%> : <%= @campaign.author.full_name %>, le </strong><%= @campaign.created_at.strftime("%d/%m/%y à %H:%M") %>. </div>
<br />
<% if !@campaign.modified_by.nil? %>
<div class="span9"> <%= t("app.actions.updated_by")%> : <%= @campaign.editor.full_name %>, le <%= @campaign.updated_at.strftime("%d/%m/%y à %H:%M") %>. </div>
<br />
<% end %>
<% end %>
</div>

<hr />

<div class="row">
<%= if !@campaign.id.nil? then f.button t("app.actions.update") + " " + Campaign.model_name.human, :type => :submit, :class =>"btn btn-primary",:id =>"campaign_validate_form" else f.button t("app.actions.create") + " " + Campaign.model_name.human, :type => :submit, :class =>"btn btn-primary",:id =>"campaign_validate_form" end%>
<%= if !@campaign.id.nil? then link_to '<i class="fa fa-trash fa-lg"></i>'.html_safe, @campaign, :title => t("app.actions.destroy") + " " + Campaign.model_name.human, :confirm => t("app.message.confirm.delete"), :class=>"pull-right btn", :method => :delete end %>
</div>

<% end %>
10 changes: 10 additions & 0 deletions app/views/campaigns/_title.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="container-fluid" style="margin-top: 1em;">
<div class="well">
<div class="row-fluid">
<div class="span9">
<h1><i class="fa fa-university"></i> <%= Campaign.model_name.human(count: 2).capitalize %></h1>
</div>
<%= link_to '<i class="fa fa-plus-circle fa-3x action-icon"></i>'.html_safe, new_campaign_path ,{:title => t("app.actions.create") + " " + Campaign.model_name.human, :class=>"pull-right"}%>
</div>
</div>
</div>
10 changes: 10 additions & 0 deletions app/views/campaigns/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="container-fluid">
<div class="well" style="margin-top: 1em;">
<div class="row-fluid">
<div class="span9">
<h1><i class="fa fa-university"></i> <%= t('app.actions.edition').capitalize + " " + Campaign.model_name.human.capitalize %><%= @campaign.id %></h1>
</div>
<%= link_to '<i class="fa fa-arrow-circle-left fa-3x action-icon"></i>'.html_safe, :back ,{:title => t("link.back"), :class=>"pull-right"} %>
</div>
</div> <%= render 'form' %>
</div>
34 changes: 34 additions & 0 deletions app/views/campaigns/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<%= render 'title', :locals => { :title => @title }%>

<div class="container-fluid">

<table class="table table-bordered table-striped">
<thead>
<tr>
<th class="text-center"><%= t("helpers.label.campaign.name") %></th>
<th class="text-center"><%= t("helpers.label.campaign.date_begin") %></th>
<th class="text-center"><%= t("helpers.label.campaign.date_end") %></th>
<th class="text-center"><%= t("helpers.label.campaign.event_type") %></th>
<th class="text-center"><%= t("app.actions.edit") %></th>
</tr>
</thead>

<tbody>
<% @campaigns.each do |campaign| %>
<tr>
<td class="text-center"><%= campaign.name %></td>
<td class="text-center"><%= campaign.date_begin.strftime(t('datetime.formats.default')) unless campaign.date_begin.blank? %></td>
<td class="text-center"><%= campaign.date_end.strftime(t('datetime.formats.default')) unless campaign.date_end.blank? %></td>
<td class="text-center"><%= campaign.event_type.full_type unless campaign.event_type.blank? %></td>
<td class="text-center"><%= link_to '<i class="fa fa-edit fa-2x action-icon"></i>'.html_safe, edit_campaign_path(campaign),{:title => t("app.actions.edit") + " " + Campaign.model_name.human} %></td>
</tr>
<% end %>
</tbody>

</table>

<br />

<%= paginate @campaigns %>

</div>
13 changes: 13 additions & 0 deletions app/views/campaigns/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="container-fluid">

<div class="well" style="margin-top: 1em;">
<div class="row-fluid">
<div class="span9">
<h1><i class="fa fa-university"></i> <%= t('app.default.new_female').capitalize + " " + Campaign.model_name.human.capitalize %></h1>
</div>
<%= link_to '<i class="fa fa-arrow-circle-left fa-3x action-icon"></i>'.html_safe, :back ,{:title => t('link.back'), :class=>"pull-right"}%>
</div>
</div>

<%= render 'form' %>
</div>
14 changes: 14 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ en:
x_seconds:
one: 1 second
other: ! '%{count} seconds'
formats:
default: ! '%Y-%m-%d %H:%M'
prompts:
day: Day
hour: Hour
Expand Down Expand Up @@ -144,6 +146,12 @@ en:
submit: Save %{model}
update: Update %{model}
label:
campaign:
name: 'Name'
date_begin: 'Start at'
date_end: 'End at'
event_type: 'Event type'
notes: 'Notes'
campaign_completed_stage:
name: 'Name'
percentage: 'Percentage'
Expand Down Expand Up @@ -313,6 +321,9 @@ en:
payment_term:
one: 'payment term'
other: 'payment terms'
campaign:
one: 'campaign'
other: 'campaigns'



Expand Down Expand Up @@ -536,6 +547,9 @@ en:
created_payment_term: "Payment term has been created !"
updated_payment_term: "Payment term has been updated !"
deleted_payment_term: "Payment term has been deleted !"
created_campaign: "Campaign has been created !"
updated_campaign: "Campaign has been updated !"
deleted_campaign: "Campaign has been deleted !"
confirm_import_account: "%{nbr} accounts have been imported !"
confirm_import_contact: "%{nbr} contacts have been imported !"
delete_import: "Import have been deleted !"
Expand Down
Loading

0 comments on commit 3317985

Please sign in to comment.