mercredi 17 août 2016

show custom records in activeadmin index page

I want to show venues in a conditional approach on index page in activeadmin like if a current_admin_user role would be super_admin then he can view all records, if current_admin_user role would be city_manager then he can also view all records but if current_admin_user role would be venue_manager then he can only view there venues.

I am not able to do this, I have venues as per conditional approach but let me know how to use venues variable to list out venues on index page

index do 
    if current_admin_user.is_super_admin?
        venues = Venue.all
    elsif current_admin_user.is_city_manager?
        venues = Venue.all
    elsif current_admin_user.is_venue_manager?  
        venues = current_admin_user.user.venues
    else
        venues = Venue.all
    end
    selectable_column
    id_column
    column :title    
    actions
end

Thanks In Advance.

Following users on Ruby on Rails

I decided to upgrade code of http://ift.tt/1safhxp chapter 14 Following users. My code does not work, when current_user goes on another page and open following. current_user sees himself and want to unfollow like original instagram. unfollow button does not work

show_follow.html.erb
<div class="line-gray-modals"></div>
  <% if @users.any? %>
    <% @users.each do |user| %>
  <ul class="follow-list">
    <li><%= image_tag(user.avatar.url(:thumb), :class => "img-cirkle") %></li>
    <li >
      <div class="modal-follow-ns" > <div class="bold"> <%= link_to user.username, user %></div> <br>
      <div class="modal-follow-ns2"><%= link_to truncate(user.status, :length => 50) %></div></div>
    </li>
    <li class="button-modal">
      <% unless user == current_user %>
        <div id="follow_form">
          <% if current_user.following?(user) %>
            <%= form_for(current_user.relationships.find_by(followed_id: user),
                           html: { method: :delete },
                           remote: true) do |f| %>
              <%= f.submit "Unfollow", class: "btn btn-large" %>
            <% end %>
          <% else %>
            <%= form_for(current_user.relationships.build(followed_id: user.id),
                                                         remote: true) do |f| %>
              <div><%= f.hidden_field :followed_id %></div>
              <%= f.submit "Follow", class: "btn btn-large btn-primary" %>
            <% end %>
          <% end %>
        </div>
      <% end %>
      <% if user == current_user %>
        <%= form_for(@user.relationships.find_by(followed_id: user),
                                          html: { method: :delete },
                                            remote: true) do |f| %>
          <%= f.submit "Unfollow", class: "btn btn-large" %>
        <% end %>
      <% end %>
    </li>
    <%= will_paginate %>
    <li>  <div class="line-gray-modals"></div></li>
  </ul>
<% end %>

relationships_controller.rb

   def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

terminal: Completed 500 Internal Server Error in 10ms (ActiveRecord: 1.9ms)

NoMethodError (undefined method `destroy!' for nil:NilClass):

app/models/user.rb:28:in unfollow!' app/controllers/relationships_controller.rb:15:indestroy'

mardi 16 août 2016

ruby call optional parameter

I have the following method:

def method(a=1, b=2, c=3, d=4)
  puts a
  puts b
  puts c
  puts d
end

Is there a way to output like this without checking the variables nullity? When I call the method like:

method(5,7)
=> 5
=> 2
=> 7
=> 4

Creating autimatic windows service

We are using windows server 2012 for ruby application web service. we start the web service by by using a command prompt but when someone restart the server the command prompt also going to shutdown and webservice not working, So my client want to create a new Autimatic windows service which needs to open the command prompt and execute the service when restart the server . How to achieve this ?

Rails 3.2 SSRS and SavonRD

I'm integrating my Rails 3.2 app with SSRS for reporting using Savonrb to do the Soap call to the SSRS web service. Everything is finally working and I can successfully make the following calls:

  1. LoadReport
  2. SetExecutionParameters
  3. Render

The render call takes a format parameter (in my case PDF) and returns an XML response. I thought the call would result in the report being rendered in a pdf, but it doesn't. What do I need to do with the response object to render the report?

code below:

response = @client.call(:load_report, message: {report: "/path/to/report"} )

response =  @client.call(:set_execution_parameters, message: { "Parameters" => param1} )

response =  @client.call(:render, message: {"Format" => "PDF"})

Thanks!

Bootstrap Accordion Collapse not indexing divs correctly

When trying to index divs so that the accordion buttons will collapse correctly, it's indexing every first contact in each category in my json file as 0 instead of indexing every contact no matter the category consecutively.

Here's my erb file:

<% @contacts.each do |category, hash| %>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
  <div class="panel-heading">
  <h3 class="panel-title"><%= category %></h3>
  </div>
    <div class="panel-body">
      <ul class="list-group">
        <% hash.each_with_index do |contact, index| %>
        <li class="list-group-item" style="border: none">
        <button type="button" class="btn btn-info" data-toggle="collapse" data-parent="#accordion" data-target="#collapse<%= index %>"><%= contact['name'] %><%= index %></button>
          <div id="collapse<%= index %>" class="panel-collapse collapse">
            <%= contact['email'] %></br><%= contact['ext'] %>
          </div>
        </li>
      <% end %>
      </ul>
  </div>
</div>
</div>
<% end %>

json file: (It's indexing the first name in each group i.e. "Manager, Team Lead" as 0. I want Rob = 0, Terry = 1, Ben = 3, and so on.

{
    "Manager":[
    {
      "name": "Rob",
      "email": "Rob@com",
      "ext": "ext:"
    }
  ],
  "Team Lead":[
    {
      "name": "Terry",
      "email": "Terry@com",
      "ext": "ext"
    }
    ],
  "Unix Admins":[
      {
        "name": "Ben",
        "email": "Benjamin@com",
        "ext": "ext"
      },
      {
        "name": "Prashant",
        "email": "Prashant@com",
        "ext": "ext"
      },
      {
        "name": "Harshil
        "email": "Harshil@com",
        "ext": "ext"
      },
      {
        "name": "Laxmikanth",
        "email": "Laxmikanth@com",
        "ext": "ext"
      },
      {
        "name": "Mary",
        "email": "Mary@com",
        "ext": "ext"
      }
    ],
    "On-Call Contact":[
      {
        "name": "SYSTEM ADMIN UNIX",
        "email": "Unix@com",
        "ext": "ext"
      }
    ]
}

.rb file:

get '/contact' do
    contact = Contacts.new
    @contacts = contact.getContacts
    @contacts.each { |s| puts "get /contact found contact #{s.last.first['name']}" }
    erb :contact

lundi 15 août 2016

how can i convert a date format from PST to UTC

Hello I am trying to convert time object from PST to UTC

time = "2016-08-15 11:51:27 America/Los_Angeles"

What I am trying is:

utc = Time.parse(time).utc

but it doesn't returns the correct time it returns.

# 2016-08-15 11:51:27 UTC

what's wrong here?