Permalink
Browse files

selectable_attrからrails依存部分を分けて別のgemにしました。

  • Loading branch information...
akm2000
akm2000 committed Jan 26, 2009
1 parent 4e80635 commit a1d0175b1870b997d9787744780b1aacd6e9c97e
View
@@ -0,0 +1 @@
+/selectable_attr_test.sqlite3.db
View
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Takeshi AKIMA
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
13 README
@@ -0,0 +1,13 @@
+SelectableAttrRails
+==============
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2008 Takeshi AKIMA, released under the MIT license
View
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the selectable_attr plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the selectable_attr plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'SelectableAttr'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
23 init.rb
@@ -0,0 +1,23 @@
+require 'selectable_attr'
+
+module SelectableAttr
+ class Enum
+ include SelectableAttr::DbLoadable
+ end
+end
+
+class ActiveRecord::Base
+ include SelectableAttr::Base
+end
+
+class ActionView::Base
+ include SelectableAttr::Helpers::SelectHelper::Base
+ include SelectableAttr::Helpers::CheckBoxGroupHelper::Base
+ include SelectableAttr::Helpers::RadioButtonGroupHelper::Base
+end
+
+class ActionView::Helpers::FormBuilder
+ include SelectableAttr::Helpers::SelectHelper::FormBuilder
+ include SelectableAttr::Helpers::CheckBoxGroupHelper::FormBuilder
+ include SelectableAttr::Helpers::RadioButtonGroupHelper::FormBuilder
+end
View
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,7 @@
+# -*- coding: utf-8 -*-
+require 'selectable_attr'
+
+module SelectableAttrRails
+ autoload :Helpers, 'selectable_attr_rails/helpers'
+ autoload :DbLoadable, 'selectable_attr_rails/db_loadable'
+end
@@ -0,0 +1,65 @@
+module SelectableAttrRails
+ module DbLoadable
+ def update_by(*args, &block)
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ options = {:when => :first_time}.update(options)
+ @sql_to_update = block_given? ? block : args.first
+ @update_timing = options[:when]
+ self.extend(InstanceMethods) unless respond_to?(:update_entries)
+ end
+
+ module Entry
+ attr_accessor :name_from_db
+ def name
+ @name_from_db || super
+ end
+ end
+
+ module InstanceMethods
+ def entries
+ update_entries if must_be_updated?
+ @entries
+ end
+
+ def must_be_updated?
+ return false if @update_timing == :never
+ return true if @update_timing == :everytime
+ end
+
+ def update_entries
+ unless @original_entries
+ @original_entries = @entries.dup
+ @original_entries.each do |entry|
+ entry.extend(SelectableAttr::DbLoadable::Entry)
+ end
+ end
+ records = nil
+ if @sql_to_update.respond_to?(:call)
+ records = @sql_to_update.call
+ else
+ @connection ||= ActiveRecord::Base.connection
+ records = @connection.select_rows(@sql_to_update)
+ end
+
+ new_entries = []
+ records.each do |r|
+ if entry = @original_entries.detect{|entry| entry.id == r.first}
+ entry.name_from_db = r.last unless r.last.blank?
+ new_entries << entry
+ else
+ entry = SelectableAttr::Enum::Entry.new(self, r.first, "entry_#{r.first}".to_sym, r.last)
+ new_entries << entry
+ end
+ end
+ @original_entries.each do |entry|
+ unless new_entries.include?(entry)
+ entry.name_from_db = nil
+ new_entries << entry if entry.defined_in_code
+ end
+ end
+ @entries = new_entries
+ end
+ end
+
+ end
+end
@@ -0,0 +1,7 @@
+module SelectableAttrRails
+ module Helpers
+ autoload :SelectHelper, 'selectable_attr_rails/helpers/select_helper'
+ autoload :CheckBoxGroupHelper, 'selectable_attr_rails/helpers/check_box_group_helper'
+ autoload :RadioButtonGroupHelper, 'selectable_attr_rails/helpers/radio_button_group_helper'
+ end
+end
@@ -0,0 +1,55 @@
+module SelectableAttrRails::Helpers
+ class AbstractSelectionBuilder
+ attr_reader :entry_hash
+
+ def initialize(object, object_name, method, options, template)
+ @object, @object_name, @method = object, object_name, method
+ @base_name = @object.class.enum_base_name(method.to_s)
+ @template = template
+ @entry_hash = nil
+ @options = options || {}
+ @entry_hash_array = @options[:entry_hash_array]
+ end
+
+ def enum_hash_array_from_object
+ base_name = @object.class.enum_base_name(@method.to_s)
+ @object.send("#{base_name}_hash_array")
+ end
+
+ def enum_hash_array_from_class
+ base_name = @object.class.enum_base_name(@method.to_s)
+ @object.class.send("#{base_name}_hash_array")
+ end
+
+ def tag_id(tag)
+ result = nil
+ tag.scan(/ id\=\"(.*?)\"/){|s|result = s}
+ return result
+ end
+
+ def add_class_name(options, class_name)
+ (options ||= {}).stringify_keys!
+ (options['class'] ||= '') << ' ' << class_name
+ options
+ end
+
+ def camelize_keys(hash, first_letter = :lower)
+ result = {}
+ hash.each{|key, value|result[key.to_s.camelize(first_letter)] = value}
+ result
+ end
+
+ def update_options(dest, *options_array)
+ result = dest || {}
+ options_array.each do |options|
+ next unless options
+ if class_name = options.delete(:class)
+ add_class_name(result, class_name)
+ end
+ result.update(options)
+ end
+ result
+ end
+
+ end
+end
@@ -0,0 +1,63 @@
+require 'selectable_attr_rails/helpers/abstract_selection_helper'
+module SelectableAttrRails::Helpers
+ module CheckBoxGroupHelper
+ class Builder < SelectableAttrRails::Helpers::AbstractSelectionBuilder
+
+ def initialize(object, object_name, method, options, template)
+ super(object, object_name, method, options, template)
+ @entry_hash_array ||= enum_hash_array_from_object
+ @param_name = "#{@base_name}_ids"
+ @check_box_options = @options.delete(:check_box) || {}
+ end
+
+ def each(&block)
+ @entry_hash_array.each do |@entry_hash|
+ @tag_value = @entry_hash[:id].to_s.gsub(/\s/, "_").gsub(/\W/, "")
+ @check_box_id = "#{@object_name}_#{@param_name}_#{@tag_value}"
+ yield(self)
+ end
+ end
+
+ def check_box(options = nil)
+ options = update_options({
+ :id => @check_box_id, :type => 'checkbox', :value => @tag_value,
+ :name => "#{@object_name}[#{@param_name}][]"
+ }, @check_box_options, options)
+ options[:checked] = 'checked' if @entry_hash[:select]
+ @template.content_tag("input", nil, options)
+ end
+
+ def label(text = nil, options = nil)
+ @template.content_tag("label", text || @entry_hash[:name],
+ update_options({:for => @check_box_id}, options))
+ end
+ end
+
+ module Base
+ def check_box_group(object_name, method, options = nil, &block)
+ object = (options || {})[:object] || instance_variable_get("@#{object_name}")
+ builder = Builder.new(object, object_name, method, options, @template)
+ if block_given?
+ yield(builder)
+ return nil
+ else
+ result = ''
+ builder.each do
+ result << builder.check_box
+ result << '&nbsp;'
+ result << builder.label
+ result << '&nbsp;'
+ end
+ return result
+ end
+ end
+ end
+
+ module FormBuilder
+ def check_box_group(method, options = nil, &block)
+ @template.check_box_group(@object_name, method,
+ (options || {}).merge(:object => @object), &block)
+ end
+ end
+ end
+end
@@ -0,0 +1,55 @@
+module SelectableAttrRails::Helpers
+ module RadioButtonGroupHelper
+ class Builder < SelectableAttrRails::Helpers::AbstractSelectionBuilder
+
+ def initialize(object, object_name, method, options, template)
+ super(object, object_name, method, options, template)
+ @entry_hash_array ||= enum_hash_array_from_class
+ end
+
+ def each(&block)
+ @entry_hash_array.each do |entry_hash|
+ @entry_hash = entry_hash
+ tag_value = @entry_hash[:id].to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
+ @radio_button_id = "#{@object_name}_#{@method}_#{tag_value}"
+ yield(self)
+ end
+ end
+
+ def radio_button(options = nil)
+ @template.radio_button(@object_name, @method, @entry_hash[:id],
+ update_options({:id => @radio_button_id}, options))
+ end
+
+ def label(text = nil, options = nil)
+ @template.content_tag("label", text || @entry_hash[:name],
+ update_options({:for => @radio_button_id}, options))
+ end
+ end
+
+ module Base
+ def radio_button_group(object_name, method, options = nil, &block)
+ object = (options || {})[:object] || instance_variable_get("@#{object_name}")
+ builder = Builder.new(object, object_name, method, options, self)
+ if block_given?
+ yield(builder)
+ return nil
+ else
+ result = ''
+ builder.each do
+ result << builder.radio_button
+ result << builder.label
+ end
+ return result
+ end
+ end
+ end
+
+ module FormBuilder
+ def radio_button_group(method, options = nil, &block)
+ @template.radio_button_group(@object_name, method,
+ (options || {}).merge(:object => @object), &block)
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit a1d0175

Please sign in to comment.