Active_admin and :filter - ruby-on-rails-3.1

The issue lies in the following: filter :contact, :as => :string works successfully if I type the id for the contact. But that's not practical when you have 2000+ contacts. How can I successfully filter :contact, as => :string but have it search for :name instead of :id.
I have tried the following with no success:
filter :contact, :as => :string, :collection => proc {Contact.where(:name => 'Paul' )}
filter :contact, :as => :string, :collection => proc { (Contact.order.all).resources{|c| [c.name]}}
Note: my repository can be found here.
Model: order.rb
belongs_to :contact
Migration:
def change
create_table :orders do |t|
t.string :tag
t.text :description
t.string :technician_id
t.string :status
t.string :type
t.string :contact_id
t.string :business_id
t.timestamps
end
end
admin/orders/ - orders.rb
filter :business
filter :contact, :as => :string, :collection => proc { (Contact.order.all).resources{|c| [c.name]}}
filter :tag
filter :description, :label => "Status"
filter :created_at
index do
column :business
column :contact
column :tag
column :status
column :description, :sortable => false do |order|
truncate(order.description, :length => 30)
end

Activeadmin uses meta_search gem, so try this:
filter :contact_name, :as => :string

Related

has_many through association error while relating users and projects

I have used has_many :through association for many-to-many relationship between two models namely user and project, but it seems as there is some mistake in it, as the queries which worked well in many-to-one relationship is throwing errors. Can someone check it please! The schema file is as below:
ActiveRecord::Schema.define(version: 2018_12_19_170114) do
create_table "project_users", force: :cascade do |t|
t.integer "user_id"
t.integer "project_id"
t.index ["project_id"], name: "index_project_users_on_project_id"
t.index ["user_id"], name: "index_project_users_on_user_id"
end
create_table "projects", force: :cascade do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "picture"
t.string "title"
t.index ["user_id"], name: "index_projects_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.integer "enroll"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.boolean "admin", default: false
t.index ["email"], name: "index_users_on_email", unique: true
end
end
The user.rb file includes the code:
class User < ApplicationRecord
has_many :project_users
has_many :projects, through: :project_users
def feed
projects
end
end
The project.rb file has the code:
class Project < ApplicationRecord
has_many :project_users
has_many :users, :through => :project_users
The project_user.rb file has the code:
class ProjectUser < ApplicationRecord
belongs_to :project, foreign_key: true
belongs_to :user, foreign_key: true
end
class StaticPagesController < ApplicationController
def home
if logged_in?
#project = current_user.projects.build
#feed_items = current_user.feed.paginate(page: params[:page])
end
end
The error is thrown by the code:
<% if #user.projects.any? %>
<h3>Projects (<%= #user.projects.count() %>)</h3>
The error is:
SQLite3::SQLException: no such column: project_users.true: SELECT 1 AS one FROM "projects" INNER JOIN "project_users" ON "projects"."id" = "project_users"."true" WHERE "project_users"."user_id" = ? LIMIT ?
This is a terrible worded question, as the comments have suggested, please include error messages otherwise it's impossible to debug.
At a glance, I can see the following issues in your schema:
Tables are named in plural form, e.g. project_users
Foreign keys are in singular form, e.g. user_id
Use the t.references :user helper instead of t.integer :user_id. You can then pass index: true, foreign_key: true options to setup indices and foreign keys.

Paperclip setup nightmare. advice?

We have projects and photos in separate tables and models. Here is our schema:
create_table "projects", force: :cascade do |t|
t.string "title"
t.integer "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "description"
t.boolean "retired", default: false
t.integer "tenant_id"
t.date "requested_by"
t.integer "repayment_rate"
t.date "repayment_begin"
end
create_table "photos", force: :cascade do |t|
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "project_id"
end
In the project model I have:
has_attached_file :image
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]
Is this acceptable as a way to setup paperclip? Or do projects and photos have to be all in one table?
yes, it's acceptable, but your has_attached_file and validates_attachment_content_type must be into photos model, not into project model.
# project.rb
class Project < ActiveRecord::Base
has_many :photos
end
# photo.rb
class Photo < ActiveRecord::Base
belongs_to :project
has_attached_file :image #, styles: { svga: ["800x600!", :jpg], big: ["500x375!", :jpg], small: ["190x143!", :jpg], thumb: ["95x71!", :jpg] }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"] # or do_not_validate_attachment_file_type :image
end

Rails 4: Group Text Message Strategy - Passing Multiple Records to a Controller

My Rails app works by letting Users create Clients and then send or schedule text_messages using Twilio. Creating one Client and then generating one text_message record works great. The User owns Clients which owns Text_Message.
My next feature to tackle is to allow a User to choose which Clients they want to send a group text message too. I want to generate a text_message record for each Client (with the same content and scheduled_date). Saved text_messages are getting added to a Sidekiq queue.
I need some help (please) on the strategy on this. How do I implement Group_Text while keeping the code dry and continuing to generate records for the text_message table? How do I pass multiple selected clients from a form/view to a controller to create the records while using the logic from the Text_Message model? Thanks for the pointers!
Relevant code
schema.rb
ActiveRecord::Schema.define(version: 20141207214913) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "action_plans", force: true do |t|
t.integer "client_id"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "action_plans", ["client_id"], name: "index_action_plans_on_client_id", using: :btree
create_table "clients", force: true do |t|
t.integer "user_id"
t.string "first_name", null: false
t.string "last_name", null: false
t.string "phone", null: false
t.string "salesforce_id", null: false
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.text "contact_id"
end
add_index "clients", ["user_id"], name: "index_clients_on_user_id", using: :btree
create_table "coach_emails", force: true do |t|
t.integer "user_id"
t.string "email", null: false
t.string "coach_firstname"
t.string "client_lastname"
t.string "client_firstname"
t.text "content", null: false
t.boolean "sentstatus"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "coach_emails", ["user_id"], name: "index_coach_emails_on_user_id", using: :btree
create_table "goals", force: true do |t|
t.integer "action_plan_id"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "goals", ["action_plan_id"], name: "index_goals_on_action_plan_id", using: :btree
create_table "steps", force: true do |t|
t.integer "goal_id"
t.text "description"
t.date "due_by"
t.boolean "complete", default: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "steps", ["goal_id"], name: "index_steps_on_goal_id", using: :btree
create_table "text_messages", force: true do |t|
t.integer "client_id"
t.text "content"
t.boolean "incoming_message"
t.datetime "created_at"
t.datetime "updated_at"
t.date "scheduled_date"
t.boolean "sentstatus"
t.integer "step_id"
t.string "phone"
end
add_index "text_messages", ["client_id"], name: "index_text_messages_on_client_id", using: :btree
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.string "first_name"
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
t.string "title"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
text_message.rb
require 'twilio-ruby'
require 'date'
class TextMessage < ActiveRecord::Base
belongs_to :client, dependent: :destroy
belongs_to :step, dependent: :destroy
has_many :coach_emails
before_save :grab_phone
def grab_phone
self.phone = phone
end
def send_text_message(message, phone)
twilio_sid = ENV["TWILIO_ACCT_SID"]
twilio_token = ENV["TWILIO_AUTH_TOKEN"]
twilio_phone_number = ENV["TWILIO_PHONE_NUMBER"]
begin
#twilio_client = Twilio::REST::Client.new(twilio_sid, twilio_token)
#twilio_client.account.sms.messages.create(
:from => "+1#{twilio_phone_number}",
:to => phone,
:body => message)
rescue Twilio::REST::RequestError => e
puts e.message
end
if e != "400" || e != "500"
self.sentstatus = true
end
self.save!
end
end
routes.rb
Rails.application.routes.draw do
require 'sidekiq/web'
devise_for :users, :path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout'},
:controllers => { registrations: 'registrations' }
authenticate :user do
mount Sidekiq::Web => '/sidekiq'
end
resources :clients do
resources :action_plans, shallow: true, except: [:index]
end
resources :action_plans, shallow: true, only: [] do
resources :goals, shallow: true, except: [:index]
end
resources :goals, shallow: true, only: [] do
resources :steps, shallow: true, except: [:index, :destroy]
end
resources :steps, shallow: true, only: [] do
resources :text_messages, shallow: true, except: [:index, :destroy]
end
get "text_messages/receive"
match '/receivetext' => 'text_messages#receive', :via => :post
resources :clients do
collection do
post :welcome
end
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
I figured out that I need to add to the TextMessage controller (#groupnew and #groupcreate).
New Routes
get 'group_new' => 'text_messages#group_new'
post 'group_create' => 'text_messages#group_create'
Updated TextMessage Controller
def group_new
#clients = current_user.clients.order(:last_name)
#text_message = TextMessage.new
end
def group_create
client_hash = params[:client_id]
client_hash.each do |client|
#client = Client.find(client)
content = params[:text_message][:content]
#text_message = #client.text_messages.build(text_message_params)
#text_message.incoming_message = false
#text_message.sentstatus = false
#text_message.phone = #client.phone
if #text_message.scheduled_date == nil
#text_message.scheduled_date = Date.today
# print time in Pacific time
#text_message.scheduled_time = Time.now.in_time_zone("Pacific Time (US & Canada)")
#text_message.send_text_message(#text_message.content, #text_message.phone)
else
#text_message.save
end
end
redirect_to clients_path
end
Group text now works :)

How to make multiple file uploading via CarrierWave and Sinatra?

I'm not first who is asking and maybe not last. How to implement multiple uploading with CarrierWave in Sinatra? I'm using this code in action:
post '/create' do
params.delete 'submit'
d = Dcmnt.new(
:published => params[:published],
:name => params[:name],
:description => params[:description],
:created_at => Time.now
)
d.attachment = params[:attachments]
d.save
redirect '/success'
end
With this model:
class Dcmnt
include Mongoid::Document
store_in collection: 'dcmnts'
field :published, type: Boolean
field :name, type: String
field :description, type: String
field :created_at, type: Date
mount_uploader :attachment, Uploader, type: String
end
And this form:
%form{:method => "post", :action => "/create", :enctype => "multipart/form-data"}
%input{:type => "checkbox", :name => "published", :value => "true"} Published?
%input{:name => "name"}
%textarea{:name => "description", :rows => "5"}
%div.form-group
%label Attachments
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
%input{:type => "file", :name => "attachments[]"}
Also CW's configuration:
class Uploader < CarrierWave::Uploader::Base
storage :file
def store_dir
'attachments/' + model.id
end
end
Looks correct, but doesn't work. When I'm trying to upload couple of files either single file, Pow returns me no implicit conversion of nil into String on d.attachment = params[:attachments] line. And I can't figure out why. Can you help me with this?

Short-form for validates_exclusion_of

If
validates_presence_of :login,
:message => 'How do you expect to login?'
has a short-form variant
validates :login, :presence => { :message => 'How do you expect to login?' },
what is the short-form version of
validates_exclusion_of :login, :in => ['admin'],
:message => "\"{{value}}\" is reserved."
so I can also have a custom message?
validates :login, :exclusion => { :in => ['admin'], :message => "%{value} is reserved" }

Resources