Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first set of passing tests

  • Loading branch information...
commit 574d1da3b219dd0f4929c84443a1dc9485901109 0 parents
Guy Boertje authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Guy Boertje
+
+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.
13 README
@@ -0,0 +1,13 @@
+HasFlexibleFields
+=================
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2009 Guy Boertje, released under the MIT license
23 Rakefile
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the has_flexible_fields plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/has_*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the has_flexible_fields plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'HasFlexibleFields'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1  init.rb
@@ -0,0 +1 @@
+ActiveRecord::Base.send :include, Has::FlexibleFields
1  install.rb
@@ -0,0 +1 @@
+# Install hook code here
21 lib/flexifield.rb
@@ -0,0 +1,21 @@
+# To change this template, choose Tools | Templates
+# and open the template in the editor.
+
+class Flexifield < ActiveRecord::Base
+
+ belongs_to :flexifield_set, :polymorphic => true
+ belongs_to :flexifield_def, :include => 'flexifield_def_entries'
+
+ delegate :to_ff_alias, :to_ff_field, :ff_aliases, :ff_fields, :to => :flexifield_def
+
+ def ff_alias_value ff_alias
+ ff_field = to_ff_field ff_alias
+ if ff_field
+ send ff_field
+ else
+ raise ArgumentError, "Flexifield alias: #{ff_alias} not found in flexifeld def mapping"
+ end
+
+ end
+
+end
31 lib/flexifield_def.rb
@@ -0,0 +1,31 @@
+class FlexifieldDef < ActiveRecord::Base
+
+ has_many :flexifield_def_entries, :class_name => 'FlexifieldDefEntry', :order => 'ordering', :dependent => :destroy
+ accepts_nested_attributes_for :flexifield_def_entries
+
+ def to_ff_field ff_alias
+ idx = nil
+ ff_aliases.each_with_index do |c,i|
+ idx = i if c == ff_alias
+ end
+ idx ? flexifield_def_entries[idx].to_ff_field : nil
+ end
+
+ def to_ff_alias ff_field
+ idx = nil
+ ff_fields.each_with_index do |c,i|
+ idx = i if c == ff_field
+ end
+ idx ? flexifield_def_entries[idx].to_ff_alias : nil
+ end
+
+ def ff_aliases
+ flexifield_def_entries.nil? ? [] : flexifield_def_entries.map(&:flexifield_alias)
+ end
+
+ def ff_fields
+ flexifield_def_entries.nil? ? [] : flexifield_def_entries.map(&:flexifield_name)
+ end
+
+
+end
13 lib/flexifield_def_entry.rb
@@ -0,0 +1,13 @@
+class FlexifieldDefEntry < ActiveRecord::Base
+ belongs_to :flexifield_def
+ validates_presence_of :flexifield_name, :flexifield_alias
+
+ def to_ff_field ff_alias = nil
+ (ff_alias.nil? || flexifield_alias == ff_alias) ? flexifield_name : nil
+ end
+ def to_ff_alias ff_field = nil
+ (ff_field.nil? || flexifield_name == ff_field) ? flexifield_alias : nil
+ end
+
+
+end
72 lib/has/flexible_fields.rb
@@ -0,0 +1,72 @@
+# Has FlexibleFields
+#
+# Copyright (c) 2009 Guy Boertje <gboertje AT gowebtop DOT com>
+# see README for license
+module Has #:nodoc:
+ module FlexibleFields #:nodoc:
+
+
+
+ def self.included(base) # :nodoc:
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def has_flexiblefields options = {}
+ unless includes_flexiblefields?
+
+ has_one :flexifield, :as => :flexifield_set, :dependent => :destroy
+ delegate :ff_alias_value, :to_ff_alias, :ff_aliases, :to_ff_field, :ff_fields, :to => :flexifield
+
+ accepts_nested_attributes_for :flexifield
+
+ after_create :create_flexifield
+ after_save :save_flexifield
+
+# if options[:some_option]
+#
+# end
+
+ end
+
+ include InstanceMethods
+
+ end
+ def includes_flexiblefields?
+ self.included_modules.include?(InstanceMethods)
+ end
+ end
+
+ module InstanceMethods
+ def has_ff_def?
+ flexifield.nil? ? false : !flexifield.flexifield_def_id.nil?
+ end
+
+ def assign_ff_def ff_def_id
+ self.flexifield.flexifield_def_id = ff_def_id
+ save_flexifield
+ end
+
+ def assign_ff_value ff_alias, value
+ ff_field = to_ff_field ff_alias
+ if ff_field
+ self.flexifield.send "#{ff_field}=", value
+ save_flexifield
+ else
+ raise ArgumentError, "Flexifield alias: #{ff_alias} not found in flexifeld def mapping"
+ end
+ end
+
+ protected
+
+ def save_flexifield
+ self.flexifield.save
+ end
+
+ def create_flexifield
+ self.flexifield = Flexifield.new()
+ end
+
+ end
+ end
+end
1  tasks/has_flexible_fields.rb
@@ -0,0 +1 @@
+# HasFlexibleFields
4 tasks/has_flexible_fields_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :has_flexible_fields do
+# # Task goes here
+# end
BIN  test/db/has_flexible_fields.sqlite3.db
Binary file not shown
59 test/db/schema.rb
@@ -0,0 +1,59 @@
+ActiveRecord::Migration.verbose = false
+
+ActiveRecord::Schema.define do
+
+ create_table "flexifield_def_entries", :force => true do |t|
+ t.integer "flexifield_def_id", :null => false
+ t.string "flexifield_name", :null => false
+ t.string "flexifield_alias", :null => false
+ t.integer "ordering"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "flexifield_def_entries", ["flexifield_def_id", "flexifield_name"], :name => "idx_ffonceperspecial", :unique => true
+ add_index "flexifield_def_entries", ["ordering"], :name => "idx_ordering"
+
+ create_table "flexifield_defs", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "flexifields", :force => true do |t|
+ t.integer "flexifield_def_id"
+ t.integer "flexifield_set_id"
+ t.string "flexifield_set_type"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.string "ffs_01"
+ t.string "ffs_02"
+ t.string "ffs_03"
+ t.string "ffs_04"
+ t.string "ffs_05"
+ t.string "ffs_06"
+ t.string "ffs_07"
+ t.string "ffs_08"
+ t.string "ffs_09"
+ t.string "ffs_10"
+ t.string "ffs_11"
+ t.string "ffs_12"
+ t.string "ffs_13"
+ t.string "ffs_14"
+ t.string "ffs_15"
+ t.string "ffs_16"
+ end
+
+ add_index "flexifields", ["flexifield_def_id"], :name => "index_flexifields_on_flexifield_def_id"
+ add_index "flexifields", ["flexifield_set_id"], :name => "index_flexifields_on_flexifield_set_id"
+ add_index "flexifields", ["flexifield_set_type"], :name => "index_flexifields_on_flexifield_set_type"
+
+ create_table "posts", :force => true do |t|
+ t.string "title"
+ t.text "body"
+ t.boolean "published"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
12 test/fixtures/flexifield_def_entries.yml
@@ -0,0 +1,12 @@
+## YAML Template.
+ffde1:
+ flexifield_def_id: 1
+ flexifield_name: ffs_01
+ flexifield_alias: excerpt
+ ordering: 1
+
+ffde2:
+ flexifield_def_id: 1
+ flexifield_name: ffs_02
+ flexifield_alias: references
+ ordering: 2
4 test/fixtures/flexifield_defs.yml
@@ -0,0 +1,4 @@
+## YAML Template.
+postset1:
+ id: 1
+ name: Post Set 1
5 test/fixtures/models.rb
@@ -0,0 +1,5 @@
+class Post < ActiveRecord::Base
+
+ has_flexiblefields
+
+end
36 test/has_flexible_fields_test.rb
@@ -0,0 +1,36 @@
+require 'test_helper'
+
+class HasFlexibleFieldsTest < ActiveSupport::TestCase
+ fixtures :flexifield_defs, :flexifield_def_entries
+
+ #this is so the test rig can have :memory: or file db and self.use_transactional_fixtures = true or false
+ def get_post assign_def = false
+ if Post.count > 0
+ returning Post.last do |post|
+ post.assign_ff_def FlexifieldDef.first.id if assign_def
+ end
+ else
+ returning Post.create!(:title => 'title1', :body => 'body1') do |post|
+ post.assign_ff_def FlexifieldDef.first.id if assign_def
+ end
+ end
+ end
+
+ def test_a_new_post_has_no_ff_def
+ post = get_post
+ assert !post.new_record?
+ assert !post.has_ff_def?
+ end
+
+ def test_b_new_post_saves_def
+ post = get_post(true)
+ assert post.has_ff_def?
+ end
+
+ def test_c_new_post_ff_aliases
+ post = get_post(true)
+ assert_equal ['excerpt','references'], post.ff_aliases
+ end
+
+
+end
964 test/log/has_flexiblefields.log
964 additions, 0 deletions not shown
37 test/test_helper.rb
@@ -0,0 +1,37 @@
+require 'test/unit'
+
+require 'rubygems'
+require 'activesupport'
+require 'active_support/test_case'
+require 'activerecord'
+require 'active_record/fixtures'
+
+dir = File.dirname(__FILE__)
+
+ENV['RAILS_ENV'] = 'test'
+
+require dir + '/../lib/flexifield'
+require dir + '/../lib/flexifield_def'
+require dir + '/../lib/flexifield_def_entry'
+require dir + '/../lib/has/flexible_fields'
+require dir + '/../init.rb'
+
+#config = { 'test' => { 'adapter' => 'sqlite3', 'dbfile' => dir + '/db/has_flexible_fields.sqlite3.db' } }
+config = { 'test' => { 'adapter' => 'sqlite3', 'database' => ':memory:' } }
+
+ActiveRecord::Base.logger = Logger.new(dir + '/log/has_flexiblefields.log')
+ActiveRecord::Base.configurations = config
+ActiveRecord::Base.establish_connection(config['test'])
+
+class ActiveSupport::TestCase #:nodoc:
+ include ActiveRecord::TestFixtures
+
+ self.use_transactional_fixtures = true
+ self.use_instantiated_fixtures = true
+ self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
+end
+
+require dir + '/fixtures/models.rb'
+require dir + '/db/schema.rb' unless Post.table_exists?
+
+
1  uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here
Please sign in to comment.
Something went wrong with that request. Please try again.