From 8b1df25d6f2ec81aab8a8cff4634d0e259803b78 Mon Sep 17 00:00:00 2001 From: Christopher Biltoft Date: Wed, 10 Sep 2014 17:30:21 +1000 Subject: [PATCH] updated version to 4.1.5 (corresponding to then current rails version) required ruby version -> 2+ rails dependency -> 4.1+ changed from test/unit to minitest removed dependence on attr_accessible & attr_protected consistent with rails 4 --- .gitignore | 109 +++++++++++++++++++++++++++++++- Gemfile.lock | 24 +++++++ Rakefile | 2 +- active_presenter.gemspec | 9 +-- lib/active_presenter/base.rb | 28 ++++---- lib/active_presenter/version.rb | 6 +- lib/tasks/gem.rake | 8 +-- test/base_test.rb | 57 +++++++++-------- test/lint_test.rb | 5 +- test/test_helper.rb | 39 ++++++------ 10 files changed, 209 insertions(+), 78 deletions(-) create mode 100644 Gemfile.lock diff --git a/.gitignore b/.gitignore index ff02aee..0ace03c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,106 @@ -Gemfile.lock -rdoc -pkg +# Compiled source # +################### +*.com +*.class +*.dll +*.exe +*.o +*.so + +# generated by bundle viz +gem_graph.png + +# temp files +,* +*~ +*~HEAD + +# from netbeans +/nbproject/* + +# from rake spec:rcov +/coverage + +coverage.data + +# from rubymine +.idea/* + +# From Linux dolphin etc +.directory + +# Packages # +############ +# it's better to unpack these files and commit the raw source +# git has its own built in compression methods +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Logs and databases # +###################### +*.log +log/*.log +*.sql +*.sqlite[3] + +# OS generated files # +###################### +.DS_Store? +ehthumbs.db +Icon? +Thumbs.db + +# Miscellaneous +###################### +rerun.txt +*~ +.~* +*.csv +.sass-cache/ +.bundle +tmp/**/* +tmp/* +doc/api +doc/app +doc/*.svg +.*.swp +*.swp +*~ +*.orig +*.pdf +components +vendor/ + +Downloads/ + +# Vagrant stuff +acceptance_config.yml +boxes/* +#/Vagrantfile +/.vagrant + +# Bundler/Rubygems +*.gem +pkg/* +tags +test/tmp/ + +# Python +*.pyc + +# Rubinius +*.rbc + +# IDE junk +.idea/* +*.iml + +# static public/assets +public/assets + diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..04684f2 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,24 @@ +PATH + remote: . + specs: + active_presenter (4.1.5) + +GEM + remote: http://rubygems.org/ + specs: + expectations (2.0.0) + mocha (>= 0.5.5) + metaclass (0.0.4) + minitest (4.7.5) + mocha (1.1.0) + metaclass (~> 0.0.1) + sqlite3 (1.3.9) + +PLATFORMS + ruby + +DEPENDENCIES + active_presenter! + expectations (~> 2.0) + minitest (~> 4.2) + sqlite3 (>= 1.3) diff --git a/Rakefile b/Rakefile index 0f4359e..2dff7e0 100644 --- a/Rakefile +++ b/Rakefile @@ -3,7 +3,7 @@ require 'rdoc/task' require File.dirname(__FILE__)+'/lib/active_presenter' Dir.glob(File.dirname(__FILE__)+'/lib/tasks/**/*.rake').each { |l| load l } -task :default => :test +task default: :test task :test do Dir['test/**/*_test.rb'].each { |l| require File.join(File.dirname(__FILE__),l)} diff --git a/active_presenter.gemspec b/active_presenter.gemspec index 3126d7c..c32f4d7 100644 --- a/active_presenter.gemspec +++ b/active_presenter.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = %q{active_presenter} - s.version = "3.2.2" + s.version = "4.1.5" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["James Golick", "Daniel Haran", "Josh Martin", "Johnno Loggie"] s.date = %q{2012-03-26} @@ -35,7 +35,8 @@ Gem::Specification.new do |s| "test/lint_test.rb", "test/test_helper.rb" ] - s.add_runtime_dependency(%q, [">= 3.0.10"]) - s.add_development_dependency(%q, [">= 2.0.0"]) - s.add_development_dependency(%q, [">= 1.3.5"]) + s.add_runtime_dependency(%q, ["~> 4.1"]) + s.add_development_dependency(%q, ["~> 2.0"]) + s.add_development_dependency(%q, ["~> 1.3"]) + s.add_development_dependency(%q, ["~> 4.2"]) end diff --git a/lib/active_presenter/base.rb b/lib/active_presenter/base.rb index 4a155b2..6e730ef 100644 --- a/lib/active_presenter/base.rb +++ b/lib/active_presenter/base.rb @@ -2,11 +2,7 @@ module ActivePresenter # Base class for presenters. See README for usage. # class Base - extend ActiveModel::Callbacks - extend ActiveModel::Naming - extend ActiveModel::Translation - include ActiveModel::MassAssignmentSecurity - include ActiveModel::Conversion + include ActiveModel::Model attr_reader :errors @@ -39,7 +35,7 @@ def self.presents(*types) send(t).errors end - # We must reassign in derrived classes rather than mutating the attribute in Base + # We must reassign in derived classes rather than mutating the attribute in Base self.presented = self.presented.merge(t => types_and_classes[t]) end end @@ -72,7 +68,7 @@ def self.human_name(options = {}) # :nodoc: :"#{klass.name.underscore}" end defaults << self.name.humanize - I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options)) + I18n.translate(defaults.shift, {scope: [:activerecord, :models], count: 1, default: defaults}.merge(options)) end # Accepts arguments in two forms. For example, if you had a SignupPresenter that presented User, and Account, you could specify arguments in the following two forms: @@ -106,7 +102,7 @@ def attributes=(attrs) attrs = attrs.stringify_keys multi_parameter_attributes = {} - attrs = sanitize_for_mass_assignment(attrs) + # attrs = sanitize_for_mass_assignment(attrs) attrs.each do |k,v| if (base_attribute = k.to_s.split("(").first) != k.to_s @@ -140,7 +136,7 @@ def method_missing(method_name, *args, &block) def valid? validated = false errors.clear - result = _run_validation_callbacks do + result = run_callbacks :validation do presented.keys.each do |type| presented_inst = send(type) next unless save?(type, presented_inst) @@ -164,7 +160,7 @@ def save saved = false ActiveRecord::Base.transaction do if valid? - _run_save_callbacks do + run_callbacks :save do saved = presented.keys.select {|key| save?(key, send(key))}.all? {|key| send(key).save} raise ActiveRecord::Rollback unless saved end @@ -181,7 +177,7 @@ def save! saved = false ActiveRecord::Base.transaction do raise ActiveRecord::RecordInvalid.new(self) unless valid? - _run_save_callbacks do + run_callbacks :save do presented.keys.select {|key| save?(key, send(key))}.all? {|key| send(key).save!} saved = true end @@ -268,10 +264,12 @@ def merge_errors(presented_inst, type) end def attribute_protected?(name) - presentable = presentable_for(name) - return false unless presentable - flat_attribute = {flatten_attribute_name(name, presentable) => ''} #remove_att... normally takes a hash, so we use a '' - presented[presentable].new.send(:sanitize_for_mass_assignment, flat_attribute).empty? + + return false + # presentable = presentable_for(name) + # return false unless presentable + # flat_attribute = {flatten_attribute_name(name, presentable) => ''} #remove_att... normally takes a hash, so we use a '' + # presented[presentable].new.send(:sanitize_for_mass_assignment, flat_attribute).empty? end end end diff --git a/lib/active_presenter/version.rb b/lib/active_presenter/version.rb index 2749df4..e9fa498 100644 --- a/lib/active_presenter/version.rb +++ b/lib/active_presenter/version.rb @@ -1,8 +1,8 @@ module ActivePresenter module VERSION - MAJOR = 3 - MINOR = 2 - TINY = 2 + MAJOR = 4 + MINOR = 1 + TINY = 5 STRING = [MAJOR, MINOR, TINY].join('.') end diff --git a/lib/tasks/gem.rake b/lib/tasks/gem.rake index ace4bb1..5c37157 100644 --- a/lib/tasks/gem.rake +++ b/lib/tasks/gem.rake @@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s| s.rubyforge_project = 'active_presenter' s.has_rdoc = true - s.required_ruby_version = '>= 1.8.5' + s.required_ruby_version = '>= 2' s.files = %w(README LICENSE Rakefile) + Dir.glob("{lib,test}/**/*") @@ -44,7 +44,7 @@ namespace :gem do namespace :upload do desc 'Upload gems (ruby & win32) to rubyforge.org' - task :rubyforge => :gem do + task rubyforge: :gem do sh 'rubyforge login' sh "rubyforge add_release giraffesoft active_presenter #{ActivePresenter::VERSION::STRING} pkg/#{spec.full_name}.gem" sh "rubyforge add_file giraffesoft active_presenter #{ActivePresenter::VERSION::STRING} pkg/#{spec.full_name}.gem" @@ -53,10 +53,10 @@ namespace :gem do end end -task :install => [:clobber, :package] do +task install: [:clobber, :package] do sh "sudo gem install pkg/#{spec.full_name}.gem" end -task :uninstall => :clean do +task uninstall: :clean do sh "sudo gem uninstall -v #{ActivePresenter::VERSION::STRING} -x #{ActivePresenter::NAME}" end diff --git a/test/base_test.rb b/test/base_test.rb index 1e1c89d..beb5721 100644 --- a/test/base_test.rb +++ b/test/base_test.rb @@ -1,4 +1,5 @@ require File.dirname(__FILE__)+'/test_helper' +require 'pp' Expectations do expect nil do @@ -9,16 +10,16 @@ SignupPresenter.new.new_record? end - expect :user => User, :account => Account do + expect user: User, account: Account do SignupPresenter.presented end expect User.create!(hash_for_user) do |u| - SignupPresenter.new(:user => u.expected).user + SignupPresenter.new(user: u.expected).user end expect true do - SignupPresenter.new(:user => nil).user.new_record? + SignupPresenter.new(user: nil).user.new_record? end expect User do @@ -26,12 +27,12 @@ end expect User.any_instance.to.receive(:login=).with('james') do - SignupPresenter.new(:user_login => 'james') + SignupPresenter.new(user_login: 'james') end # admin= should be protected from mass assignment - expect SignupPresenter.new.to.be.attribute_protected?(:user_admin) - expect SignupPresenter.new(:user_admin => true).user.not.to.be.admin? + # expect SignupPresenter.new.to.be.attribute_protected?(:user_admin) + # expect SignupPresenter.new(user_admin: true).user.not.to.be.admin? expect 'mymockvalue' do User.any_instance.stubs(:login).returns('mymockvalue') @@ -43,7 +44,7 @@ end expect SignupPresenter.new.not.to.be.valid? - expect SignupPresenter.new(:user => User.new(hash_for_user)).to.be.valid? + expect SignupPresenter.new(user: User.new(hash_for_user)).to.be.valid? expect ActiveModel::Errors do s = SignupPresenter.new @@ -128,23 +129,23 @@ ActiveRecord::Base.stubs(:transaction).yields User.any_instance.stubs(:save).returns(false) Account.any_instance.stubs(:save).returns(false) - s = SignupPresenter.new :user => User.new(hash_for_user) + s = SignupPresenter.new user: User.new(hash_for_user) s.save end expect ActiveRecord::Base.to.receive(:transaction) do - s = SignupPresenter.new(:user_login => "da", :user_password => "seekrit") + s = SignupPresenter.new(user_login: "da", user_password: "seekrit") s.save! end expect User.any_instance.to.receive(:save!) do - s = SignupPresenter.new(:user_login => "da", :user_password => "seekrit") + s = SignupPresenter.new(user_login: "da", user_password: "seekrit") s.save! end expect Account.any_instance.to.receive(:save!) do User.any_instance.stubs(:save!).returns(true) - s = SignupPresenter.new(:user_login => "da", :user_password => "seekrit") + s = SignupPresenter.new(user_login: "da", user_password: "seekrit") s.save! end @@ -152,7 +153,7 @@ SignupPresenter.new.save! end - expect SignupPresenter.new(:user => User.new(hash_for_user)).to.be.save! + expect SignupPresenter.new(user: User.new(hash_for_user)).to.be.save! expect SignupPresenter.new.to.be.respond_to?(:user_login) expect SignupPresenter.new.to.be.respond_to?(:user_password_confirmation) @@ -160,17 +161,17 @@ expect SignupPresenter.new.to.be.respond_to?(:nil?, false) # making sure it's possible to pass 2 arguments expect User.create!(hash_for_user).not.to.be.login_changed? do |user| - s = SignupPresenter.new(:user => user) - s.update_attributes :user_login => 'Something Totally Different' + s = SignupPresenter.new(user: user) + s.update_attributes user_login: 'Something Totally Different' end - expect SignupPresenter.new(:user => User.create!(hash_for_user)).to.receive(:save) do |s| - s.update_attributes :user_login => 'Something' + expect SignupPresenter.new(user: User.create!(hash_for_user)).to.receive(:save) do |s| + s.update_attributes user_login: 'Something' end expect 'Something Different' do s = SignupPresenter.new - s.update_attributes :user_login => 'Something Different' + s.update_attributes user_login: 'Something Different' s.user_login end @@ -311,7 +312,7 @@ # this should act as ActiveRecord models do expect NoMethodError do - SignupPresenter.new({:i_dont_exist=>"blah"}) + SignupPresenter.new({i_dont_exist: "blah"}) end expect false do @@ -319,11 +320,11 @@ end expect true do - SignupNoAccountPresenter.new(:user => User.new(hash_for_user), :account => nil).save + SignupNoAccountPresenter.new(user: User.new(hash_for_user), account: nil).save end expect true do - SignupNoAccountPresenter.new(:user => User.new(hash_for_user), :account => nil).save! + SignupNoAccountPresenter.new(user: User.new(hash_for_user), account: nil).save! end expect Address do @@ -331,19 +332,21 @@ end expect "123 awesome st" do - p = PresenterWithTwoAddresses.new(:secondary_address_street => "123 awesome st") + p = PresenterWithTwoAddresses.new(secondary_address_street: "123 awesome st") p.save p.secondary_address_street end # attr_protected - expect "" do - p = SignupPresenter.new(:account_secret => 'swordfish') - p.account.secret - end + # expect "" do + # p = SignupPresenter.new(account_secret: 'swordfish') + # pp " ", "p.account", p.account + # pp "p.account.secret", p.account.secret + # p.account.secret + # end expect "comment" do - p = HistoricalPresenter.new(:history_comment => 'comment', :user => User.new(hash_for_user)) + p = HistoricalPresenter.new(history_comment: 'comment', user: User.new(hash_for_user)) p.save p.history_comment end @@ -353,7 +356,7 @@ end expect true do - p = SignupPresenter.new(:user => User.new(hash_for_user)) + p = SignupPresenter.new(user: User.new(hash_for_user)) p.save p.user_login = 'something_else' p.changed? diff --git a/test/lint_test.rb b/test/lint_test.rb index 990c33e..973aef5 100644 --- a/test/lint_test.rb +++ b/test/lint_test.rb @@ -1,5 +1,6 @@ require File.dirname(__FILE__)+'/test_helper' -require 'test/unit' +# require 'test/unit' +require 'minitest' class LintTest < ActiveModel::TestCase include ActiveModel::Lint::Tests @@ -7,4 +8,4 @@ class LintTest < ActiveModel::TestCase def setup @model = SignupPresenter.new end -end \ No newline at end of file +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 405d6f9..0cf1323 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,26 +1,27 @@ require 'expectations' require 'logger' +require 'minitest/autorun' -ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}} -ActiveRecord::Base.establish_connection('sqlite3') +ActiveRecord::Base.configurations = {'sqlite3' => {adapter: 'sqlite3', database: ':memory:'}} +ActiveRecord::Base.establish_connection(:sqlite3) ActiveRecord::Base.logger = Logger.new(STDERR) ActiveRecord::Base.logger.level = Logger::WARN I18n.backend.store_translations '1337', - :activerecord => { - :models => { - :user => 'U53R' - }, - :attributes => { - :user => {:password => 'pa22w0rD'} - }, - :errors => { - :messages => { - :blank => 'c4N n07 83 8L4nK' - } - } - } + activerecord: { + models: { + user: 'U53R' + }, + attributes: { + user: {password: 'pa22w0rD'} + }, + errors: { + messages: { + blank: 'c4N n07 83 8L4nK' + } + } + } ActiveRecord::Schema.define(:version => 0) do create_table :users do |t| @@ -55,7 +56,7 @@ class User < ActiveRecord::Base validates_presence_of :login validate :presence_of_password - attr_accessible :login, :password, :birthday + # attr_accessible :login, :password, :birthday attr_accessor :password_confirmation def presence_of_password @@ -77,7 +78,7 @@ class PresenterWithTwoAddresses < ActivePresenter::Base class SignupPresenter < ActivePresenter::Base presents :account, :user - attr_protected :account_secret + # attr_protected :account_secret end class EndingWithSPresenter < ActivePresenter::Base @@ -86,7 +87,7 @@ class EndingWithSPresenter < ActivePresenter::Base class HistoricalPresenter < ActivePresenter::Base presents :user, :history - attr_accessible :history_comment + # attr_accessible :history_comment end class CantSavePresenter < ActivePresenter::Base @@ -212,7 +213,7 @@ def halt class HistoricalPresenter < ActivePresenter::Base presents :user, :history - attr_accessible :history_comment + # attr_accessible :history_comment end def hash_for_user(opts = {})