チェックボックスの値が、'true' という文字列かどうかを判定して boolean として変数に保存していたのだが、if 文の条件分岐が正しく動作しない
@hoge[:fuga] = (params[:fuga] == 'true'); if not @hoge[:fuga] # 常に true で評価されてた。アホけ? end
マジきもい。
@hoge[:fuga] = (params[:fuga] == 'true'); if not @hoge[:fuga] # 常に true で評価されてた。アホけ? end
warning: Object#id will be deprecated; use Object#object_idなんて、わけのわからん警告を出して、レコードのIDの代わりに、オブジェクトのIDを代入して、颯爽と風と共に去りぬ。
def options_for_association_conditions(association)
if association.name == :hoge
['hoge.funya = ?', 1]
elsif association.name == :fuga
['fuga.hoe = ?', 2]
else
super
end
end
class ApplicationController < ActionController::Base
before_filter :set_controller_name
def set_controller_name
session[:controller_name] = self.class;
end
end
def options_for_association_conditions(association)
if session[:controller_name] == 'MogeController'
if association.name == :hoge
['hoge.funya = ?', 1]
elsif association.name == :fuga
['fuga.hoe = ?', 2]
else
super
end
else
if association.name == :hoge
['hoge.funya = ?', 2]
elsif association.name == :fuga
['fuga.hoe = ?', 3]
else
super
end
end
end
helper :all
create table hoges (
id serial primary key,
name varchar(16)
);
create table fugas (
id serial primary key,
name varchar(16)
);
create table fugas_and_hoges (
id serial primary key,
fuga_id integer,
hoge_id integer
);
create table another_fugas_and_hoges (
id serial primary key,
fuga_id integer,
hoge_id integer
);
class FugasAndHoge < ActiveRecord::Base
belongs_to :fuga
belongs_to :hoge
end
class AnotherFugasAndHoge < ActiveRecord::Base
belongs_to :fuga
belongs_to :hoge
end
class Fuga < ActiveRecord::Base
has_many :fugas_and_hoges, :dependent => :destroy
has_many :hoges, :through => :fugas_and_hoges, :dependent => :destroy
has_many :another_fugas_and_hoges, :dependent => :destroy
has_many :another_hoges, :source => :hoge, :through => :another_fugas_and_hoges, :dependent => :destroy
end
class Hoge < ActiveRecord::Base
has_many :fugas_and_hoges, :dependent => :destroy
has_many :fugas, :through => :fugas_and_hoges, :dependent => :destroy
has_many :another_fugas_and_hoges, :dependent => :destroy
has_many :another_fugas, :source => :fuga, :through => :another_fugas_and_hoges, :dependent => :destroy
end
<% if params[:parent_model] != nil -%>
...
<% end -%>
<div>
<%= render :active_scaffold => "hoges", :constraints => { :fuga_id => @record.id }
-%>
</div>
:has_many => hoges, :dependent => :destroy
<%= stylesheet_link_tag 'jquery-ui-1.8.1.custom', :media => 'all' %>
<%= javascript_include_tag "jquery-1.4.2.min.js" %>
<script type="text/javascript">
jQuery.noConflict();
var j$ = jQuery;
</script>
<%= javascript_include_tag "jquery-ui-1.8.1.custom.min.js" %>
def tab_form_id(options = {})
options[:action] ||= params[:action]
options[:id] ||= params[:id]
options[:id] ||= params[:parent_id]
clean_id "#{controller_id}-#{options[:action]}-#{options[:id]}-tabform"
end
def tab_form_panel_id(column,options = {})
options[:action] ||= params[:action]
options[:id] ||= params[:id]
options[:id] ||= params[:parent_id]
clean_id "#{controller_id}-#{options[:action]}-#{options[:id]}-#{column.label}-tabform"
end
def override_tab_form_partial?(column)
path, partial_name = partial_pieces(override_tab_form_partial(column))
template_exists?(File.join(path, "_#{partial_name}"), true)
end
# the naming convention for overriding tab form with partials
def override_tab_form_partial(column)
"#{column.label}_tab_form"
end
def tab_form_partial_for_column(column)
if override_tab_form_partial?(column)
override_tab_form_partial(column)
else
"tab_form"
end
end
<%
# 全ての column が、add_subgroup されているかどうかで、処理を切り替える
is_tabbed = true
columns.each :for => @record do |column|
if not is_subsection? column
is_tabbed = false;
break;
end
end
-%>
<% if is_tabbed -%>
<% columns.each :for => @record do |column| -%>
<% if column.label == 'main' %>
<div class="tab-form" id="<%= tab_form_panel_id(column) %>">
<% if !override_tab_form_partial?(column) -%>
<%= render :partial => 'tab_form', :locals => { :columns => column } %>
<% end -%>
</div>
<br clear="all" />
<% end -%>
<% end -%>
<script type="text/javascript">
j$(function(){
j$("#<%= tab_form_id %>").tabs();
});
</script>
<div id="<%= tab_form_id %>" class="form" <%= 'style="display: none;"' if columns.collapsed -%>>
<ul>
<% columns.each :for => @record do |column| -%>
<% if column.label != 'main' -%>
<li class="tab-form"><a href="#<%= tab_form_panel_id(column) %>"><%= t column.label %></a></li>
<% end -%>
<% end -%>
</ul>
<% columns.each :for => @record do |column| -%>
<% if column.label != 'main' -%>
<div class="tab-form" id="<%= tab_form_panel_id(column) %>">
<%= render :partial => tab_form_partial_for_column(column), :locals => { :columns => column } %>
</div>
<% end -%>
<% end -%>
</div>
<% else -%>
<%= render :partial => 'tab_form', :locals => { :columns => columns } %>
<% end -%>
<ol class="form" <%= 'style="display: none;"' if columns.collapsed -%>>
<% columns.each :for => @record do |column| -%>
<% if is_subsection? column -%>
<li class="sub-section">
<h5><%= column.label %> (<%= link_to_visibility_toggle(:default_visible => !column.collapsed) -%>)</h5>
<%= render :partial => 'form', :locals => { :columns => column } %>
</li>
<% elsif is_subform? column and !override_form_field?(column) -%>
<li class="sub-form <%= active_scaffold_config_for(column.association.klass).subform.layout %>-sub-form <%= column.css_class unless column.css_class.nil? %>" id="<%= sub_form_id(:association => column.name) %>">
<%= render :partial => form_partial_for_column(column), :locals => { :column => column } -%>
</li>
<% else -%>
<li class="form-element <%= 'required' if column.required? %> <%= column.css_class unless column.css_class.nil? %>">
<%= render :partial => form_partial_for_column(column), :locals => { :column => column } -%>
</li>
<% end -%>
<% end -%>
</ol>
def override_form_field_will_do?(column)
respond_to?(override_form_field_will_do(column))
end
def override_form_field_will_do(column)
"#{column.name}_form_column_will_do"
end
module FormColumnHelpers
# This method decides which input to use for the given column.
# It does not do any rendering. It only decides which method is responsible for rendering.
def active_scaffold_input_for(column, scope = nil, options = {})
begin
will_do = true
options = active_scaffold_input_options(column, scope, options)
options = javascript_for_update_column(column, scope, options)
# prepare, check if form_field will be override?
if override_form_field_will_do?(column)
will_do = send(override_form_field_will_do(column), @record, options)
end
# first, check if the dev has created an override for this specific field
if will_do and override_form_field?(column)
send(override_form_field(column), @record, options)
# second, check if the dev has specified a valid form_ui for this column
elsif column.form_ui and override_input?(column.form_ui)
send(override_input(column.form_ui), column, options)
# fallback: we get to make the decision
else
....
module ListColumnHelpers
def get_column_value(record, column)
begin
will_do = true
if column_override_will_do? column
will_do = send(column_override_will_do(column), record)
end
# check for an override helper
value = if will_do and column_override? column
......
def column_override_will_do(column)
"#{column.name.to_s.gsub('?', '')}_column_will_do"
# parse out any question marks (see issue 227)
end
def column_override_will_do?(column)
respond_to?(column_override_will_do(column))
end
def name_column_will_do
record.is_a? Customer
end
def name_column(record)
"<a href='mailto:" + record[:email_address].to_s + "'>" + record[:name].to_s + "</a>";
end
...
/**
* all the behavior to respond to a text field as a search box
*/
_respond_to_text_field: function(text_field) {
// attach the events to start this party
text_field.observe('focus', this.open.bind(this));
// the autosearch event - needs to happen slightly late (keyup is later than keypress)
text_field.observe('keyup', function() {
if (!this.is_open()) return;
this.container.down('.text-input').value = text_field.value;
}.bind(this));
// keyboard navigation, if available
if (this.onkeypress) {
text_field.observe('keypress', this.onkeypress.bind(this));
}
if (this.onkeydown) {
text_field.observe('keydown', this.onkeydown.bind(this));
}
},
_use_iframe_mask: function() {
return this.container.insertAdjacentHTML ? true : false;
}
});
/**
* Adds keyboard navigation to RecordSelect objects
*/
Object.extend(RecordSelect.Abstract.prototype, {
current: null,
/**
* keyboard navigation - where to intercept the keys is up to the concrete class
* some browser does not handle KEY_XXXX. so separate to keydown event
*/
onkeypress: function(ev) {
var elem;
switch (ev.keyCode) {
case Event.KEY_SPACE:
case Event.KEY_RETURN:
if (this.current) this.current.down('a').onclick();
break;
case Event.KEY_RIGHT:
if( !this.is_open() ) this.open();
elem = this.container.down('li.pagination.next');
if (elem) elem.down('a').onclick();
break;
case Event.KEY_LEFT:
if( !this.is_open() ) this.open();
elem = this.container.down('li.pagination.previous');
if (elem) elem.down('a').onclick();
break;
case Event.KEY_ESC:
break;
case Event.KEY_TAB:
return;
default:
if( !this.is_open() ) this.open();
return;
}
Event.stop(ev); // so "enter" doesn't submit the form, among other things(?)
},
/**
* keyboard navigation - where to intercept the keys is up to the concrete class
*/
onkeydown: function(ev) {
var elem;
switch (ev.keyCode) {
var elem;
switch (ev.keyCode) {
case Event.KEY_UP:
if( !this.is_open() ) this.open();
if (this.current && this.current.up('.record-select')) elem = this.current.previous();
if (!elem) elem = this.container.getElementsBySelector('ol li.record').last();
this.highlight(elem);
break;
case Event.KEY_DOWN:
if( !this.is_open() ) this.open();
if (this.current && this.current.up('.record-select')) elem = this.current.next();
if (!elem) elem = this.container.getElementsBySelector('ol li.record').first();
this.highlight(elem);
break;
case Event.KEY_RIGHT:
if( !this.is_open() ) this.open();
elem = this.container.down('li.pagination.next');
if (elem) elem.down('a').onclick();
break;
case Event.KEY_LEFT:
if( !this.is_open() ) this.open();
elem = this.container.down('li.pagination.previous');
if (elem) elem.down('a').onclick();
break;
case Event.KEY_TAB:
case Event.KEY_ESC:
this.close();
break;
default:
return;
}
},
/**
* moves the highlight to a new object
*/
highlight: function(obj) {
if (this.current) this.current.removeClassName('current');
this.current = $(obj);
obj.addClassName('current');
}
});
def name_column(record)
"<a href='mailto:" + record[:email_address].to_s + "'>" + record[:name].to_s + "</a>";
end
def name_column(record)
if record.is_a? Customer
"<a href='mailto:" + record[:email_address].to_s + "'>" + record[:name].to_s + "</a>";
else
if record[:name] != nil
record[:name].to_s
else
''
end
end
end
def send_cookies(to)
...
else
#to['Set-Cookie'] = options['cookie'].to_s
to['Set-Cookie'] = @head['cookie'].to_s
end
...
end
def active_scaffold_input_singular_association(column, html_options)
associated = @record.send(column.association.name)
select_options = options_for_association(column.association)
select_options.unshift([ associated.to_label, associated.id ]) unless associated.nil? or select_options.find {|label, id| id == associated.id}
selected = associated.nil? ? nil : associated.id
method = column.name
#html_options[:name] += '[id]'
options = {:selected => selected, :include_blank => as_(:_select_)}
html_options.update(column.options[:html_options] || {})
html = ""
# to be disabled ... and add hidden tag
if html_options[:readonly]
html_options[:disabled] = true;
end
options.update(column.options)
html << select(:record, method, select_options.uniq, options, html_options)
if html_options[:readonly]
# この段階でないと、options[:name] が確定していない?
html << "<input type='hidden' name='#{html_options[:name]}' value='#{selected}' />"
end
html
end
create schema test;
create table foos (
id serial primary key,
bar_id integer,
name varchar(16)
);
create table bars(
id serial primary key,
name varchar(8)
);
create table test.bars (
id serial primary key,
name varchar(8)
);
class FoosController < ApplicationController
before_filter :change_schema
def change_schema
Bar.set_table_name 'test.bars'
end
end
class Hoge < ActiveRecord::Base
default_scope :order => 'id';
end
# returns options for the given association as a collection of [id, label] pairs intended for the +options_for_select+ helper.
def options_for_association(association, include_all = false)
available_records = association_options_find(association, include_all ? nil : options_for_association_conditions(association))
available_records ||= []
# available_records.sort{|a,b| a.to_label <=> b.to_label}.collect { |model| [ model.to_label, model.id ] }
# already sorted by sql. so disable sorting.
available_records.collect { |model| [ model.to_label, model.id ] }
end
ActionController::AbstractRequest.relative_url_root = ops[:prefix] if ops[:prefix]
if ops[:prefix]
if ActionController::Base.respond_to?('relative_url_root=')
ActionController::Base.relative_url_root = ops[:prefix]
# new way to set the relative URL in Rails 2.1.1
else
ActionController::AbstractRequest.relative_url_root = ops[:prefix]
end
end
config.action_links.add ....
<% if active_scaffold_config.action_links.any? { |link| link.type == :table } -%>
<div class="actions">
<!-- ここに目的のチェックボックスを作成 -->
<a position="top" class="link">
<input type="checkbox" id="view_past" value="全表示" onclick="view_all_foo()"
<%= 'checked' if(session[:pcomp] == 1) -%> />全表示</a>
<!-- ここまで -->
<% new_params = params_for(:action => :table) %>
<% active_scaffold_config.action_links.each :table do |link| -%>
<% next if controller.respond_to? link.security_method and !controller.send(link.security_method) -%>
<%= render_action_link(link, new_params) -%>
<% end -%>
<%= loading_indicator_tag(:action => :table) %>
</div>
<% end %>
<h2><%= active_scaffold_config.list.user.label %></h2>
<!-- ここからは、フィルタを実行するリクエストを送信 -->
<script type='text/javascript'>
function view_all_foo() {
var elem = document.getElementById( 'view_past' );
if( elem.checked ) {
new Ajax.Updater( 'foos-content', '/foos/set_view_all?scaffold_id=foo&page=1', { asynchronous:true, evalScripts:true } );
} else {
new Ajax.Updater( 'foos-content', '/foos/set_view_new?scaffold_id=foo&page=1', { asynchronous:true, evalScripts:true } );
}
}
</script>
<!-- ここまで -->
class FoosController < ApplicationController
layout 'application'
before_filter :authorize
record_select :per_page => 7, :search_on => [:name], :full_text_search => true
active_scaffold :foo do |config|
...
end
def set_view_all
session[:pcomp] = 1
rewrite_list
end
def set_view_new
session[:pcomp] = 0
rewrite_list
end
def conditions_for_collection
if session[:pcomp] == 0
'complete_flag = 0'
end
end
protected
...
def rewrite_list
do_list
render :active_scaffold => 'foos', :partial => 'list'
end
end
keyword: rails active_record table relation same column
class User < ActiveRecord::Base
has_many :Hoges
...
end
class Hoge < ActiveRecord::Base
belongs_to :user, :foreign_key => 'user_id'
belongs_to :manager, :class_name => 'User', :foreign_key => 'manager_id'
...
end
class HogesController < ApplicationController
active_scaffold :hoge do |config|
config.columns = [..., :user, :manager, ...]
config.columns[:user].form_ui = :select
config.columns[:manager].form_ui = :select
...
end
...
end