Permalink
Browse files

adding integration support to use simple_record for persistence

  • Loading branch information...
1 parent 4944172 commit 17d07ce8639478ec489afb124a84684db6bdbbbe @standingwave committed May 9, 2010
@@ -4,7 +4,7 @@
Padrino has a beautiful Admin management dashboard with these features:
-Orm Agnostic:: Data Adapters for Datamapper, Activerecord, Sequel, Mongomapper, Mongoid, Couchrest
+Orm Agnostic:: Data Adapters for Datamapper, Activerecord, Sequel, Mongomapper, Mongoid, Couchrest, SimpleRecord
Template Agnostic:: Erb and Haml Renderer
Authentication:: Support for Account authentication, Account Permission managment
Scaffold:: You can simply create a new "admin interface" by providing a Model
@@ -14,7 +14,7 @@ def orm
# Tell us for now wich orm we support
#
def supported_orm
- [:datamapper, :activerecord, :mongomapper, :mongoid, :couchrest, :sequel]
+ [:datamapper, :activerecord, :mongomapper, :mongoid, :couchrest, :sequel, :simple_record]
end
##
@@ -38,6 +38,7 @@ def columns
when :mongoid then @klass.fields.values
when :mongomapper then @klass.keys.values.reject { |key| key.name == "_id" } # On MongoMapper keys are an hash
when :sequel then @klass.db_schema.map { |k,v| Column.new(k, v[:type]) }
+ when :simple_record then @klass.attributes
else raise OrmError, "Adapter #{orm} is not yet supported!"
end
end
@@ -57,7 +58,7 @@ def all
def find(params=nil)
case orm
- when :activerecord, :mongomapper, :mongoid then "#{klass_name}.find(#{params})"
+ when :activerecord, :mongomapper, :mongoid, :simple_record then "#{klass_name}.find(#{params})"
when :datamapper, :couchrest then "#{klass_name}.get(#{params})"
when :sequel then "#{klass_name}[#{params}]"
else raise OrmError, "Adapter #{orm} is not yet supported!"
@@ -81,7 +82,7 @@ def save
def update_attributes(params=nil)
case orm
- when :activerecord, :mongomapper, :mongoid, :couchrest then "@#{name_singular}.update_attributes(#{params})"
+ when :activerecord, :mongomapper, :mongoid, :couchrest, :simple_record then "@#{name_singular}.update_attributes(#{params})"
when :datamapper then "@#{name_singular}.update(#{params})"
when :sequel then "@#{name_singular}.modified! && @#{name_singular}.update(#{params})"
else raise OrmError, "Adapter #{orm} is not yet supported!"
@@ -0,0 +1,54 @@
+class Account < SimpleRecord::Base
+ attr_accessor :password
+
+ # Validations
+ has_attributes :email, :role
+ has_strings :name,
+ {:name=>:password, :hashed=>true}
+
+ # validates_presence_of :email, :role
+ # validates_presence_of :password, :if => :password_required
+ # validates_presence_of :password_confirmation, :if => :password_required
+ # validates_length_of :password, :within => 4..40, :if => :password_required
+ # validates_confirmation_of :password, :if => :password_required
+ # validates_length_of :email, :within => 3..100
+ # validates_uniqueness_of :email, :case_sensitive => false
+ # validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
+ # validates_format_of :role, :with => /[A-Za-z]/
+
+ # Callbacks
+ before_save :generate_password
+
+ ##
+ # This method is for authentication purpose
+ #
+ def self.authenticate(email, password)
+ account = first(:conditions => { :email => email }) if email.present?
+ account && account.password_clean == password ? account : nil
+ end
+
+ ##
+ # This method is used by AuthenticationHelper
+ #
+ def self.find_by_id(id)
+ find(id) rescue nil
+ end
+
+ ##
+ # This method is used to retrieve the original password.
+ #
+ def password_clean
+ crypted_password.decrypt(salt)
+ end
+
+ private
+ def generate_password
+ return if password.blank?
+ self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
+ self.crypted_password = password.encrypt(self.salt)
+ end
+
+ def password_required
+ crypted_password.blank? || !password.blank?
+ end
+end
View
@@ -43,7 +43,7 @@ renderer:: haml (default), erb
stylesheet:: sass (default), less
mock:: none (default), mocha, rr
script:: none (default), jquery, prototype, mootools, rightjs, extcore
-orm:: none (default), datamapper, mongomapper, mongoid, activerecord, sequel, couchrest
+orm:: none (default), datamapper, mongomapper, mongoid, activerecord, sequel, couchrest, simple_record
To learn more about the project generator, check out the guide to
{Padrino Generators}[http://www.padrinorb.com/guides/generators].
@@ -0,0 +1,33 @@
+SIMPLERECORD = (<<-SIMPLERECORD) unless defined?(SIMPLERECORD)
+
+AWS_ACCESS_KEY_ID=
+AWS_SECRET_ACCESS_KEY=
+SimpleRecord.establish_connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY, :logger => )
+
+SIMPLERECORD
+
+def setup_orm
+ require_dependencies 'uuidtools-2.1.1', 'http_connection-1.3.0', 'xml-simple-1.0.12', 'aws-2.3.8', 'simple_record-1.2.1'
+ create_file("config/database.rb", SIMPLERECORD)
+ empty_directory('app/models')
+end
+
+SIMPLERECORD_MODEL = (<<-MODEL) unless defined?(SIMPLERECORD_MODEL)
+class !NAME! < SimpleRecord::Base
+
+end
+MODEL
+
+def create_model_file(name, fields)
+ model_path = destination_root('app/models/', "#{name.to_s.underscore}.rb")
+ model_contents = SIMPLERECORD_MODEL.gsub(/!NAME!/, name.to_s.downcase.camelize)
+ create_file(model_path, model_contents)
+end
+
+def create_model_migration(filename, name, fields)
+ # NO MIGRATION NEEDED
+end
+
+def create_migration_file(migration_name, name, columns)
+ # NO MIGRATION NEEDED
+end
@@ -25,7 +25,7 @@ def self.banner; "padrino-gen project [name] [options]"; end
class_option :dev, :desc => "Use padrino from a git checkout", :default => false, :type => :boolean
# Definitions for the available customizable components
- component_option :orm, "database engine", :aliases => '-d', :choices => [:activerecord, :datamapper, :mongomapper, :mongoid, :sequel, :couchrest], :default => :none
+ component_option :orm, "database engine", :aliases => '-d', :choices => [:activerecord, :datamapper, :mongomapper, :mongoid, :sequel, :couchrest, :simple_record], :default => :none
component_option :test, "testing framework", :aliases => '-t', :choices => [:rspec, :shoulda, :cucumber, :bacon, :testspec, :riot], :default => :rspec
component_option :mock, "mocking library", :aliases => '-m', :choices => [:mocha, :rr], :default => :none
component_option :script, "javascript library", :aliases => '-s', :choices => [:jquery, :prototype, :rightjs, :mootools, :extcore], :default => :none
@@ -146,6 +146,19 @@ def setup
assert_match_in_file(/CouchRest.database!/, '/tmp/sample_project/config/database.rb')
assert_dir_exists('/tmp/sample_project/app/models')
end
+
+ should "properly generate default for simple_record" do
+ buffer = silence_logger { generate(:project, 'sample_project', '--root=/tmp', '--orm=simple_record', '--script=none') }
+ assert_match /Applying.*?simple_record.*?orm/, buffer
+ assert_match_in_file(/gem 'uuidtools-2.1.1'/, '/tmp/sample_project/Gemfile')
+ assert_match_in_file(/gem 'http_connection-1.3.0'/, '/tmp/sample_project/Gemfile')
+ assert_match_in_file(/gem 'xml-simple-1.0.12'/, '/tmp/sample_project/Gemfile')
+ assert_match_in_file(/gem 'aws-2.3.8'/, '/tmp/sample_project/Gemfile')
+ assert_match_in_file(/gem 'simple_record-1.2.1'/, '/tmp/sample_project/Gemfile')
+
+ assert_match_in_file(/SimpleRecord.setup/, '/tmp/sample_project/config/database.rb')
+ assert_dir_exists('/tmp/sample_project/app/models')
+ end
end
context "the generator for renderer component" do

0 comments on commit 17d07ce

Please sign in to comment.