diff --git a/Gemfile b/Gemfile index 958dd05..c80ee36 100644 --- a/Gemfile +++ b/Gemfile @@ -1,16 +1,3 @@ source "http://rubygems.org" -# Specify your gem's dependencies in mini_record.gemspec -gem 'rake' -gem 'minitest' -gem 'sqlite3' -gem 'mysql2' -gem 'mysql' -gem 'pg' -gem 'activerecord', '<= 3.2' - -group :test do - gem 'foreigner', '>= 1.4.2' -end - gemspec diff --git a/README.md b/README.md index f59c4c7..f52d4b4 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,31 @@ class Fox < ActiveRecord::Base end ``` +## Versions + +### ActiveRecord 4.0.5 or lower + +If you are using ruby 2.1 and activerecord lower than or equal to 4.0.5 please +use [fork-stable](https://github.com/acdcorp/mini_record/tree/fork-stable) or +[0.3.7 tag/version](https://github.com/acdcorp/mini_record/tree/v0.3.7) + +### ActiveRecord >= 4.0.5 <= 4.2.10 and Ruby >= 2.3 + +Please use this branch [0.3-acdcorp-stable](https://github.com/acdcorp/mini_record/tree/0.3-acdcorp-stable) + + +### ActiveRecord 4.2.11 ruby 2.5.5 and 2.6.6 + +Please use branch [0.3.11-acdcorp-activerecord4.2.11](https://github.com/acdcorp/mini_record/tree/0.3.11-acdcorp-activerecord4.2.11) + +## Development + +Master is the development branch of this gem for acdcorp because we can't +change the official mini_record gem version unless we push our custom +development. + +Current version support Rails 6.0.1 + ## Author DAddYE, you can follow me on twitter [@daddye](http://twitter.com/daddye) or take a look at my site [daddye.it](http://www.daddye.it) diff --git a/lib/mini_record/auto_schema.rb b/lib/mini_record/auto_schema.rb index fa63393..1c796bb 100644 --- a/lib/mini_record/auto_schema.rb +++ b/lib/mini_record/auto_schema.rb @@ -5,6 +5,7 @@ def self.included(base) end module ClassMethods + def init_table_definition(connection) #connection.create_table(table_name) unless connection.table_exists?(table_name) @@ -12,13 +13,14 @@ def init_table_definition(connection) when 1 # Rails 3.2 and earlier ActiveRecord::ConnectionAdapters::TableDefinition.new(connection) - when 4 + when 4, -5 # Rails 4 ActiveRecord::ConnectionAdapters::TableDefinition.new(connection.native_database_types, table_name, false, {}) else raise ArgumentError, "Unsupported number of args for ActiveRecord::ConnectionAdapters::TableDefinition.new()" end + end def schema_tables @@ -72,14 +74,65 @@ def fields_in_db end end + def columns_hash + if mini_record_fake_columns + super.merge mini_record_fake_columns + else + super + end + end + + def mini_record_columns + @@_mr_columns ||= {} + @@_mr_columns[table_name] ||= {} + end + + def mini_record_fake_columns + @@_mr_fake_columns ||= {} + @@_mr_fake_columns[table_name] ||= {} + end + + # Lookup for registered ActiveRecord Types + # This in important for rails 4 and 5 due to ActiveRecord converts + # columns into object with the column type. Types depends from Connection + # Adapter. + # activerecord/lib/active_record/type/ + # active_record/connection_adapters + # initialize_type_map defined the available types depending of + # database adapter. + def lookup_cast_type(type) + connection.lookup_cast_type(type) + end + def field(*args) return unless connection? options = args.extract_options! type = options.delete(:as) || options.delete(:type) || :string index = options.delete(:index) + fake = options.delete(:fake) || false args.each do |column_name| + # add it it the mini record columns for this table so we can access + # special fields like input_as, used by form builders + mini_record_columns[column_name] = options + + if fake + # allow you to access the field on the instance object + attr_accessor column_name + # ActiveRecord 4.2.x maps columns into types. + # Create a column symbol as type produced + # undefined method `type_cast_from_database' for :[boolean|string]:Symbol + type = lookup_cast_type(type) if defined?(ActiveRecord::Type) + # create a column that column_hashes will understand (a fake row) + fake_column = ActiveRecord::ConnectionAdapters::Column.new( + column_name.to_s, nil, type, true + ) + # add it to the list of fake columns for this table + mini_record_fake_columns[column_name.to_s] = fake_column + # skip everything else as it's a fake column and don't want it in the db + next + end # Allow custom types like: # t.column :type, "ENUM('EMPLOYEE','CLIENT','SUPERUSER','DEVELOPER')" diff --git a/lib/mini_record/version.rb b/lib/mini_record/version.rb index a0881d2..92aac4b 100644 --- a/lib/mini_record/version.rb +++ b/lib/mini_record/version.rb @@ -1,3 +1,3 @@ module MiniRecord - VERSION = "0.3.7" + VERSION = "0.3.10" end diff --git a/mini_record.gemspec b/mini_record.gemspec index ae7866b..eaf3784 100644 --- a/mini_record.gemspec +++ b/mini_record.gemspec @@ -24,5 +24,12 @@ Gem::Specification.new do |s| # specify any dependencies here; for example: # s.add_development_dependency "rspec" - s.add_dependency "activerecord", ">=3.2.0" + s.add_runtime_dependency "activerecord", '~> 6.1.0' + + s.add_development_dependency 'rake' + s.add_development_dependency 'minitest' + s.add_development_dependency 'sqlite3' + s.add_development_dependency 'mysql2' + s.add_development_dependency 'pg' + s.add_development_dependency 'foreigner', '>= 1.4.2' end