ActiveAdmin - how to find the id of the default scope? - activeadmin

How can I find the id of the default scope in ActiveAdmin?
eg:
ActiveAdmin.register User do
scope :foo, default: true
scope :bar
controller do
def my_method
id_of_default_scope = ??? # do something that returns :foo
end
end
end

Related

Rails 4.1 Nested Attributes and Fields For Getting Unpermitted Parameters and Not Saving

Research: Rails 4.0 beta, fields_for not accepting pluralized model_name in one-to-many association, Rails 4 Nested Attributes with fields_for Don't Save to Database
First, let's get the most common problem out of the way: incorrectly named attributes parameters for strong parameters. Mine is correctly plural.
class AdultsController < ApplicationController
...
def update
authorize #user
respond_to do |format|
if #user.update_attributes(user_params)
format.html { redirect_to unit_adult_path(#unit, #user), notice: "#{#user.full_name} was successfully updated." }
else
format.html { render action: 'edit' }
end
end
end
def user_params
params.require(:adult).permit(:first_name, :last_name, phones_attributes: [])
end
end
And my models are setup correctly
class User < ActiveRecord::Base
has_many :phones, dependent: :destroy
accepts_nested_attributes_for :phones, allow_destroy: true, reject_if: proc { |a| a["number"].blank? }
end
class Phone < ActiveRecord::Base
belongs_to :user, touch: true
end
And the view
# adult/_form.html.haml
= bootstrap_form_for [#unit, #user] do |f|
= f.text_field :first_name, control_col: 'col-md-4'
= f.text_field :last_name, control_col: 'col-md-4'
= f.fields_for :phones do |f_phone|
= f_phone.form_group do
= f_phone.select :kind, options_for_phones, hide_label: true, layout: :default
= f_phone.phone_field :number, hide_label: true, layout: :default
= f_phone.check_box :_destroy, label: 'remove'
But, when I submit the User form to save
Started PATCH "/units/2/adults/1" for 127.0.0.1 at 2014-07-11 15:20:17 -0700
Processing by AdultsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pDjDVSiEs5qqHLqnbxQMeGWDOUGvhXPPvgyRGmitmps=", "adult"=>{"first_name"=>"Karl", "last_name"=>"Smith", "phones_attributes"=>{"0"=>{"kind"=>"other", "number"=>"888.1212", "_destroy"=>"0", "id"=>"173"}, "1"=>{"kind"=>"mobile", "number"=>"888.1212", "_destroy"=>"0", "id"=>"174"}} }, "commit"=>"Update Adult", "unit_id"=>"2", "id"=>"1"}
Unpermitted parameters: phones_attributes
I don't understand why the nested data is being rejected by the strong parameter evaluation. It looks correct to me.
The one thing I do notice is that the params data for "phones_attributes" value is a HASH not an ARRAY. In the user_params, phones_attributes: [] looks like it expecting an ARRAY. So I changed it to a HASH.
def user_params
params.require(:adult).permit(:first_name, :last_name, phones_attributes: {})
end
But now I get the following error.
Unpermitted parameters: 0, 1
So I tried specifying the field names in the "phones_attributes" array.
def user_params
params.require(:adult).permit(:first_name, :last_name, phones_attributes: [:id, :kind, :number])
end
And I still get.
Unpermitted parameters: phones_attributes
I know I must be missing something small, but I can't find my error.
EDIT: all my nested attributes forms do not work. Not 100% sure when they stopped, but ones that worked previously no longer work and have not been modified.
Figured this out. I was using javascript to copy the phone fields (kind, number) to make a new set of inputs available for entry. The script was adding non numeric characters to part of the field id, and this was causing rails to ignore all the submitted phone_attributes.
For the next person that comes along...
When fields_for renders out the fields, it will index each input name for uniqueness when the submitted post data is converted to params. In the example below, this number field
<input id="adult_phones_attributes_0_number" name="adult[phones_attributes][0][number]" type="tel" value="7773331111">
will look something like this when converted to params
"phones_attributes"=>{"0"=>{"number"=>"7773331111"}}
The hash key of "0" comes from the index created by fields_for. It's the "[0]" portion of the name.
In versions of rails past, if that nested attributes params hash key was not a number, the k/v pair was just ignored. Well now with strong parameters (I'm guessing the culprit), it will reject the entire "phones_attributes" hash.
My script was copying the input field, doing a regex on the html to change the "[0]" index to a random number. But sometimes it would replace it will non-digit characters. And this was causing the problem.

make column of associated resource in index table non-linked

Okay, using ActiveAdmin (0.6.3) and am able to use the following code to get a caller's location attribute to appear in the table of callers. The name of each location appears as a link to the "show" action for the location. The "show" action and result is not useful for my application. I want to remove the link but keep the text. Help? Thanks :)
ActiveAdmin.register Caller do
index do
column 'Location', :location, :sortable => 'locations.name'
end
controller do
def scoped_collection
resource_class.includes(:location)
end
end
end
class Caller < ActiveRecord::Base
attr_accessible :active, :assignedname, :callingnumber, :description, :location_id, :lookupcount, :lastlookuptime
belongs_to :location
end
class Location < ActiveRecord::Base
attr_accessible :active, :description, :name, :callers_attributes
has_many :callers, dependent: :destroy
end
Try this.
index do
column 'Location', :sortable => 'locations.name' do |caller|
caller.location.name
end
end

Rubymotion Teacup variable scope

Following the Tweets example of Teacup, I'm trying to access a variable outside of the layout block
class WindowController < TeacupWindowController
stylesheet :pref_window
layout do
#loginButton = subview(
NSButton, :loginButton,
state: NSOffState,
buttonType: NSSwitchButton,
action: 'login',
target: self,
)
puts #loginButton.class
end
puts #loginButton.class
the first puts returns NSButton class, but the second one returns Nil class.
how can I access #loginButton if I need to programmatically make changes to it?
For example:
#loginButton.setState(NSOnState)
doesn't work outside the layout block.
You can use an attr_accessor on the WindowController, then in the layout block you can use self.loginButton and it will be assigned on the WindowController giving you access to the button.
I'm also assuming the second puts is actually in another method and this is just example code.
class WindowController < TeacupWindowController
stylesheet :pref_window
attr_accessor :loginButton
layout do
self.loginButton = subview(
NSButton, :loginButton,
state: NSOffState,
buttonType: NSSwitchButton,
action: 'login',
target: self,
)
puts self.loginButton.class
end
puts self.loginButton.class
end

Passing parameter via PageFactory

I'm using page-object gem with watir and RSpec to test my web application.
Here is my page object.
require 'page-object'
class UserEditPage
include PageObject
page_url "#{$context}/user/edit?id=#{#params[:id]}"
text_field :user_id, name: 'userName'
text_field :pw, name: 'password'
text_field :pw_retype, name: 'password2'
# snip uninteresting stuff
button :submit, index: 0
end
I would like to parameterize :id in page_url via PageObject::PageFactory like this:
visit UserEditPage, using_params: {id: 1000} do |page|
# operation on page
end
This document implies that above usage is possible, but I couldn't understand how exactly this is achieved.
Question: how can I pass parameter id from visit method to UserEditPage?
Running code results in
***/user/edit_page.rb:8:in `<class:UserEditPage>': undefined method `[]' for nil:NilClass (NoMethodError)
from ***/user/edit_page.rb:5:in `<top (required)>'
probably because #params is nil when evaluating page_url.
Changing using_params: {id: 1000} to id: 1000 with no luck.
The page_url needs to be:
page_url "#{$context}/user/edit?id=<%=params[:id]%>"
Note how the parameters need to be included differently - ie <%=params[:id]%> instead of #{params[:id]}.
The problem is that the page_url is evaluated when the page object class is evaluated. This means that when the class is evaluated, params[:id] must already exist. This is not going to be true when you want to include the parameters dynamically.
The page object gem uses ERB to allow you to create a template, which allows substitution of the params later - ie when calling visit.
In summary, when creating a page_url:
Use string interpolation if the value is known when the page object class is evaluated.
Use ERB if the value is to be dynamically specified on visit.
Working Example
Below is a working example of the dynamic parameters.
require 'watir-webdriver'
require 'page-object'
$context = 'http://testurl.com'
class UserEditPage
include PageObject
page_url "#{$context}/user/edit?id=<%=params[:id]%>"
end
include PageObject::PageFactory
#browser = Watir::Browser.new
visit UserEditPage, using_params: {id: 1000} do |page|
p page.current_url
#=> "http://testurl.com/user/edit?id=1000"
end

rails_admin searchable association

I am using rails_admin together with globalize3 and cannot get searchable associations to work. Here are the models (Person has_one/belongs_to Name has_many/belongs_to NameTranslation):
class Person < ActiveRecord::Base
has_one :name, inverse_of: :person
end
class Name < ActiveRecord::Base
belongs_to :person, inverse_of: :name
translates :first_name, :last_name
has_many :name_translations, inverse_of: :name, dependent: :destroy
end
class NameTranslation < ActiveRecord::Base
belongs_to :name, inverse_of: :name_translations
end
The NameTranslation model is coming from globalize3, it contains the same attributes as name (first_name and last_name) plus locale and name_id,.
In config/initializers/rails_admin.rb I have
config.model Person do
list do
field :name do
searchable name_translations: :last_name
end
end
end
Then, in the GUI, when I add a filter on name, I get:
SQLite3::SQLException: no such column: name_translations.last_name: SELECT "people".* FROM "people" WHERE (((name_translations.last_name LIKE '%freud%'))) ORDER BY people.id desc LIMIT 20 OFFSET 0
Obviously, rails_admin is looking for a column named name_translations.last_name in people instead of joining/including names and name_translations - why?
What I need rails_admin to do is this, working in irb:
>> Person.joins( name: :name_translations ).where('name_translations.last_name like "test"')
which generates the following SQL:
SELECT "people".* FROM "people" INNER JOIN "names" ON "names"."person_id" = "people"."id" INNER JOIN "name_translations" ON "name_translations"."name_id" = "names"."id" WHERE (name_translations.last_name like "test")
Can this be done in rails_admin? Thanks for your help...
From this thread, I followed Nick Roosevelt's suggestion and it worked for my case
class Room < ActiveRecord:Base
has_many :time_slots
end
class TimeSlot < ActiveRecord::Base
belongs_to :room
rails_admin do
list do
field :day do
searchable true
end
# field :room do
# searchable room: :name
# end
field :room do
searchable [{Room => :name}]
queryable true
end
end
end
end
I tried searchable room: :name and it was not working, but searchable [{Room => :name}] seem to make it work.
I had a similar problem with a has one relationship.
The way I solved it was to set a default_scope on the model and join it with the associated table (it is was the only way I could get rails admin to join these two tables).
I also had to set queryable true on the associated field.
Imagine that you had to search only inside the name association, then here's how it would work:
class Person < ActiveRecord::Base
has_one :name, inverse_of: :person
default_scope { eager_load(:name) }
end
config.model Person do
list do
field :name do
queryable true
searchable [:column1, :column2, ..]
end
end
end
However, you need to search through the has many association and I don't know whether that approach would still work, but here's a guess:
class Person < ActiveRecord::Base
has_one :name, inverse_of: :person
has_many :name_translations, through: :name
default_scope { eager_load(:name_translations) }
end
config.model Person do
list do
field :name_translations do
queryable true
searchable :last_name
end
end
end

Resources