cancan gem

1. ability.rb

class Ability
include CanCan::Ability

def initialize(receiver) // model name
receiver ||= Receiver.new # guest user (not logged in)
if receiver.role_name == ‘Admin’
can :manage, :all
elsif receiver.role_name == ‘Receiver’
can :read, :all
can :update, Compliant
else
can :read, :all
can :create, Compliant
end
end
end

2. role.rb

class Role < ActiveRecord::Base

# Relationships
has_many  :receiver
end

3. receiver.rb

class Receiver < ActiveRecord::Base

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :timeoutable

# RelationShip
has_many    :compliant_receivers, :dependent => :destroy
belongs_to :company
belongs_to :role
has_many :compliants, through: :compliant_receivers

# Delegation
delegate :name, to: :role, prefix: true, allow_nil: true
end

4. active_admin.rb

config.current_user_method = :current_receiver

5. application_controller.rb
def current_ability
@current_ability ||= Ability.new(current_receiver)
end

Advertisements

nested attributes

Whenever you want to select more than one value from dropdown using nested attributes for reducing memory area.Commonly if you want store more than one value per field for single row in table, we can use nested attributes for reducing memory.

Relationships

class Compliant < ActiveRecord::Base
protokoll :unique_id, :pattern => “whistle#####”

# Relationships
 has_many     :compliant_receivers
belongs_to  :report_type
belongs_to    :compliance_status
belongs_to     :company
has_many    :receivers, through: :compliant_receivers

# Delegates
delegate :name, :to => :report_type, prefix: true, allow_nil: true
delegate :name, :to => :compliance_status, prefix: true, allow_nil: true

# Validations
validates :title, presence: true
validates :title, uniqueness: true
validates :report_type_id, presence: true
validates :reason, presence: true
validates :idea, presence: true
validates :description, presence: true
validates :terms, presence: true

# Nested Attributes
accepts_nested_attributes_for :compliant_receivers
end

class Receiver < ActiveRecord::Base
# Include default devise modules. Others available are:
# :lockable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :timeoutable

# RelationShip
belongs_to :company
has_one :role
has_many    :compliant_receivers
has_many    :compliants, through: :compliant_receivers

end

class CompliantReceiver < ActiveRecord::Base
 belongs_to :compliant
    belongs_to :receiver
end

2.accepts nested attributes

In compliant.rb
accepts_nested_attributes_for :compliant_receivers

3.compliant_controller
pass parameters like “compliant_receivers_attributes: [:id, :receiver_id]”

def compliant_params
params.require(:compliant).permit(:title,:report_type_id,:reason,:idea,:description,:terms,:unique_id,:expiry_date,:compliance_status_id, :company_id, compliant_receivers_attributes: [:id, :receiver_id]) //if cmpliant_controller means no need mention compliant_id like compliant_receivers_attributes: [:id, :receiver_id] or if its receiver controller means no need mention receiver_id just you can mention id like compliant_receivers_attributes: [:compliant_id,:id]
end

4.compliants/_form

<%= f.fields_for :compliant_receivers,(f.object.compliant_receivers.present? ? f.object.compliant_receivers : CompliantReceiver.new) do |receivers| %>
                      <%= render ‘compliant_receiver_fields’, compliant: @compliant, f: receivers %>
                    <% end %>

gem ‘axlsx_rails’

If you want more details about axlsx_rails gem please follow the given link

https://code.google.com/p/axlsx/source/browse/axlsx_examples.wiki?repo=wiki

https://github.com/straydogstudio/axlsx_rails

Example:-

risk_dashboard_controller.rb

# Method for Generating Report
def get_report
@risks = current_company.risks
@chart_name = params[:chart_name]
@type = params[:type]
respond_to do |format|
format.xlsx {
response.headers[‘Content-Disposition’] = ‘inline; filename=”RiskReport.xlsx”‘
response.headers[‘Authorization’] = ‘BasicCustom’
}
end
end

get_report.xlsx.axlsx

wb = xlsx_package.workbook
wb.styles do |s|
main_title = s.add_style(:alignment => { :horizontal => :center }, :bold => true, :sz => 18, :bg_color => “7f8c8d”)
head_title = s.add_style(:alignment => { :horizontal => :center }, :bold => true, :sz => 16, :bg_color => “1abc9c” ,:border => Axlsx::STYLE_THIN_BORDER)
chart_style = s.add_style(:bg_color => “1abc9c”)
content_style = s.add_style(:alignment => { :horizontal => :center }, :bold => true)
chart_color =  %w(e67e22, 16a085, 3498db, 7f8c8d, d35400, 1abc9c, c0392b, 27ae60, 2c3e50, f1c40f)

if @chart_name == ‘Risk Locations’
wb.add_worksheet(name: “Index”) do |sheet|
sheet.add_row [‘Location’,’Risk Count’], :style => head_title
sheet.column_widths 30, 18
sheet.add_table “A1:B4”, :name => ‘Build Matrix’, :style_info => { :name => “TableStyleMedium10” }
@location = current_company.locations.joins(:risk_locations).select(“locations.name as name, locations.id as lid, count(risk_id )as count”).group(:name,:lid)
row_no = 1
@location.each do |l|
row_no = row_no + 1
sheet.add_row [l.name,l.count]
sheet.add_hyperlink :location => “‘#{l.name}’!A1”, :ref => “A#{row_no}”, :target => :sheet
wb.add_worksheet(name: l.name) do |s|
s.add_row [‘Subject’,’Status’,’Risk Score’,’Risk Owner’,’Mitigator’,’Reviwer’,’Team’,’Department’,’Description’], :style => head_title
s.column_widths 30,10,15,15,15,15,15,15,30
@risk = @risks.joins(:risk_locations).where(“location_id = ? “, l.lid)
@risk.each do |risk|
value = risk.risk_scoring.calculated_risk.present? ? risk.risk_scoring.calculated_risk : risk.risk_scoring.custom_value
s.add_row [risk.subject, risk.risk_status_name, value, risk.risk_owner_user_name, risk.risk_mitigator_user_name, risk.risk_reviewer_user_name, risk.team_name, risk.department_name, risk.assessment], :style => content_style
end
end
end

if(@type==’Bar’)
sheet.add_chart(Axlsx::Bar3DChart,:title => “Location Vs Risk”) do |chart|
chart.start_at 7, sheet.rows.size – 2
chart.end_at 17, sheet.rows.size + 16
chart.add_series :data => sheet.cols[1][(1..-1)], :labels => sheet.cols[0][(1..-1)],:colors => chart_color
chart.catAxis.title =’Count’
chart.valAxis.title =  @chart_name
chart.d_lbls.show_val = true
end
sheet.merge_cells(“H3:Q20”)
elsif(@type==’Pie’)

sheet.add_chart(Axlsx::Pie3DChart, :title => “Location Vs Risk”) do |chart|
chart.start_at 7, sheet.rows.size – 2
chart.end_at 14, sheet.rows.size + 16
chart.add_series :data => sheet.cols[1][(1..-1)], :labels => sheet.cols[0][(1..-1)], :colors => chart_color
chart.d_lbls.show_val = true
chart.d_lbls.show_val = true
end
sheet.merge_cells(“H3:N20”)
else

sheet.add_chart(Axlsx::LineChart, :title => “Location Vs Risk”) do |chart|
chart.start_at 6, sheet.rows.size – 2
chart.end_at 16, sheet.rows.size + 16
chart.add_series :data => sheet.cols[1][(1..-1)], :labels => sheet.cols[0][(1..-1)], :color =>’2980b9′
chart.catAxis.title = @chart_name
chart.valAxis.title = ‘Count’
chart.d_lbls.show_val = true
chart.d_lbls.show_val = true
end
sheet.merge_cells(“G3:P20″)
end
end

 

_chart.html.erb

<ul class=”dropdown-menu” role=”menu”>
<li><a href=”#” onclick=”risk_chart_destroy(<%= chart.id%>);”>Delete</a></li>
<li><%= link_to “Report”, get_report_path(chart_name: chart.x_axis,type: chart.chart_type, format: ‘xlsx’)%></li>

 

Where you have to write common functionality in Rails

In rails we can write only once it automatically inheriting sub classes,details given below

In Controller

application_controller.rb -> here you can write common functionality,then no need write same functionality in some other controllers.

In Views

layouts/application.html.erb

In Javascript

app/assets/javascripts/views/application.js

In CSS

app/assets/stylesheets/application.css.scss

 

what is the use of in rails

<%yield%> always we are using in application.html.erb.This <%yield%> variable always redirecting to ” root ‘home#index’ “, what your mentioning as root file in config/routes.rb.

Example:-

<!– Header –>
<%= render ‘layouts/header’ if receiver_signed_in? %>
<!– Flash message –>
<%= render ‘layouts/flash’ if receiver_signed_in? %>
<%= yield %>
<!– Footer –>
<%= render ‘layouts/footer’ if receiver_signed_in? %>

How to redirect to a specific page on successful sign up using rails devise gem?

How to redirect to a specific page on successful sign up using rails devise gem?

devise gem always redirecting root path,you can see config/routes.rb like  root ‘home#index’
So, you can redirect to a specific page after successful login insert the following code to file called “application_controller.rb” like,

protected

# Modification of signin path
def after_sign_in_path_for(resource)
dashboards_path      //you can specify any path here
end