ruby-wpdb 1.2 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,67 +0,0 @@
1
- Sequel.inflections do |inflect|
2
- # Unless we tell Sequel otherwise, it will try to inflect the singular
3
- # of "postmeta" using the "data" -> "datum" rule, leaving us with the
4
- # bizarre "postmetum".
5
- inflect.uncountable 'postmeta'
6
- end
7
-
8
- module WPDB
9
- class Post < Sequel::Model(:"#{WPDB.prefix}posts")
10
- include Termable
11
-
12
- plugin :validation_helpers
13
- plugin :sluggable, :source => :post_title, :target => :post_name
14
-
15
- many_to_one :parent, :class => self, :key => :post_parent
16
- one_to_many :children,
17
- :key => :post_parent,
18
- :class => self do |ds|
19
- ds.where(:post_type => ['attachment', 'revision']).invert
20
- .where(:post_parent => self.ID)
21
- end
22
- one_to_many :revisions,
23
- :key => :post_parent,
24
- :class => self,
25
- :conditions => { :post_type => 'revision' }
26
- one_to_many :attachments,
27
- :key => :post_parent,
28
- :class => self,
29
- :conditions => { :post_type => 'attachment' }
30
-
31
- one_to_many :postmeta, :class => :'WPDB::PostMeta'
32
- many_to_one :author, :key => :post_author, :class => :'WPDB::User'
33
- one_to_many :comments, :key => :comment_post_ID, :class => :'WPDB::Comment'
34
-
35
- one_to_many :termrelationships,
36
- :key => :object_id,
37
- :key_method => :obj_id,
38
- :class => 'WPDB::TermRelationship'
39
- many_to_many :termtaxonomy,
40
- :left_key => :object_id,
41
- :right_key => :term_taxonomy_id,
42
- :join_table => :"#{WPDB.prefix}term_relationships",
43
- :class => 'WPDB::TermTaxonomy'
44
-
45
- def validate
46
- super
47
- validates_presence [:post_title, :post_type, :post_status]
48
- validates_unique :post_name
49
- end
50
-
51
- def before_validation
52
- self.post_type ||= "post"
53
- self.post_status ||= "draft"
54
- self.post_parent ||= 0
55
- self.menu_order ||= 0
56
- self.comment_status ||= "open"
57
- self.ping_status ||= WPDB::Option.get_option("default_ping_status")
58
- self.post_date ||= Time.now
59
- self.post_date_gmt ||= Time.now.utc
60
- super
61
- end
62
- end
63
-
64
- class PostMeta < Sequel::Model(:"#{WPDB.prefix}postmeta")
65
- many_to_one :posts, :class => :'WPDB::Post'
66
- end
67
- end
@@ -1,49 +0,0 @@
1
- module WPDB
2
- class Term < Sequel::Model(:"#{WPDB.prefix}terms")
3
- plugin :validation_helpers
4
- plugin :sluggable, :source => :name, :target => :slug
5
-
6
- one_to_many :termtaxonomies, :class => 'WPDB::TermTaxonomy'
7
-
8
- def validate
9
- super
10
- validates_presence :name
11
- end
12
- end
13
-
14
- class TermTaxonomy < Sequel::Model(:"#{WPDB.prefix}term_taxonomy")
15
- many_to_one :terms, :class => 'WPDB::Term'
16
- one_to_many :termrelationships, :class => 'WPDB::TermRelationship'
17
- many_to_many :posts, :left_key => :term_taxonomy_id, :right_key => :object_id, :join_table => :"#{WPDB.prefix}term_relationships", :class => 'WPDB::Post'
18
- many_to_many :links, :left_key => :term_taxonomy_id, :right_key => :object_id, :join_table => :"#{WPDB.prefix}term_relationships", :class => 'WPDB::Link'
19
- end
20
-
21
- class TermRelationship < Sequel::Model(:"#{WPDB.prefix}term_relationships")
22
- def_column_alias(:obj_id, :object_id)
23
-
24
- many_to_one :termtaxonomy, :class => 'WPDB::TermTaxonomy'
25
- many_to_one :posts, :class => 'WPDB::Post'
26
- many_to_one :links, :class => 'WPDB::Link'
27
- end
28
-
29
- module Termable
30
- # For objects that have a relationship with termtaxonomies, this
31
- # module can be mixed in and gives the ability to add a term
32
- # directly to the model, rather than creating the relationship
33
- # yourself. Used by Post and Link.
34
- def add_term(term, taxonomy)
35
- if term.respond_to?(:term_id)
36
- term_id = term.term_id
37
- else
38
- term_id = term.to_i
39
- end
40
-
41
- term_taxonomy = WPDB::TermTaxonomy.where(:term_id => term_id, :taxonomy => taxonomy).first
42
- unless term_taxonomy
43
- term_taxonomy = WPDB::TermTaxonomy.create(:term_id => term_id, :taxonomy => taxonomy)
44
- end
45
-
46
- add_termtaxonomy(term_taxonomy)
47
- end
48
- end
49
- end
@@ -1,40 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe WPDB::Comment do
4
- before do
5
- @comment = WPDB::Comment.create(
6
- :comment_author => 'Testy Testerson',
7
- :comment_author_email => 'testy@example.com',
8
- :comment_content => 'Test'
9
- )
10
- end
11
-
12
- it "creates a new comment" do
13
- assert @comment.comment_ID
14
- end
15
-
16
- it "attaches comments to posts" do
17
- post = WPDB::Post.create(:post_title => 'test', :post_author => 1)
18
- assert post.ID
19
-
20
- post.add_comment(@comment)
21
- assert @comment.post
22
- assert post.comments.length
23
- assert_equal post.comments.first.comment_post_ID, post.ID
24
-
25
- post.destroy
26
- end
27
-
28
- it "adds commentmeta" do
29
- @comment.add_commentmeta(:meta_key => 'test', :meta_value => 'test')
30
-
31
- comment = WPDB::Comment.where(:comment_ID => @comment.comment_ID).first
32
- assert comment.commentmeta.length > 0
33
- assert_equal 'test', comment.commentmeta.first.meta_key
34
- assert_equal 'test', comment.commentmeta.first.meta_value
35
- end
36
-
37
- after do
38
- @comment.destroy
39
- end
40
- end
data/test/config_test.rb DELETED
@@ -1,20 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- require "open-uri"
4
-
5
- describe "Config" do
6
- it "correctly parses the sample wp-config" do
7
- sample_config = Pathname(__FILE__) + ".." + ".." + "data" + "wp-config-sample.php"
8
- live_url = "https://raw.githubusercontent.com/WordPress/WordPress/master/wp-config-sample.php"
9
- open(live_url) do |live|
10
- File.open(sample_config, "w") do |f|
11
- f.write live.read
12
- end
13
- end
14
-
15
- config = WPDB::Config::WPConfig.new(sample_config).config
16
-
17
- assert_equal "mysql2://username_here:password_here@localhost/database_name_here", config[:uri]
18
- assert_equal "wp_", config[:prefix]
19
- end
20
- end
@@ -1,25 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe WPDB::GravityForms do
4
- before do
5
- @form = WPDB::GravityForms::Form.first
6
- @class_name = WPDB.camelize(@form.title).to_sym
7
- end
8
-
9
- it "fetches a form" do
10
- assert @form.id
11
- end
12
-
13
- it "associates leads with a form" do
14
- assert @form.leads.length
15
- end
16
-
17
- it "associates lead detail with a lead" do
18
- assert @form.leads.first.details.length
19
- end
20
-
21
- it "lazily loads models for forms" do
22
- klass = WPDB::GravityForms.const_get(@class_name)
23
- assert_equal Class, klass.class
24
- end
25
- end
data/test/links_test.rb DELETED
@@ -1,28 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe WPDB::Link do
4
- before do
5
- @link = WPDB::Link.create(:link_url => 'http://example.com', :link_name => 'Example')
6
- end
7
-
8
- it "creates links" do
9
- assert @link.link_id
10
- end
11
-
12
- it "can attach terms to links" do
13
- term = WPDB::Term.create(:name => 'terming links')
14
- @link.add_term(term, 'category')
15
- @link.save
16
-
17
- assert @link.link_id
18
- assert @link.termtaxonomy.length
19
- assert_equal term.term_id, @link.termtaxonomy.first.term_id
20
- assert_equal 'category', @link.termtaxonomy.first.taxonomy
21
-
22
- term.destroy
23
- end
24
-
25
- after do
26
- @link.destroy
27
- end
28
- end
data/test/options_test.rb DELETED
@@ -1,26 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe WPDB::Option do
4
- before do
5
- @option = WPDB::Option.create(:option_name => 'test', :option_value => 'test')
6
- end
7
-
8
- it "creates options" do
9
- assert @option.option_id
10
- end
11
-
12
- it "enforces the uniqueness of option names" do
13
- assert_raises Sequel::UniqueConstraintViolation do
14
- WPDB::Option.create(:option_name => 'test', :option_value => 'test')
15
- end
16
- end
17
-
18
- it "has a shorthand for fetching options" do
19
- assert_equal 'test', WPDB::Option.get_option('test')
20
- assert_equal nil, WPDB::Option.get_option('non-existent-key')
21
- end
22
-
23
- after do
24
- @option.destroy
25
- end
26
- end
data/test/posts_test.rb DELETED
@@ -1,50 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe WPDB::Post do
4
- before do
5
- @post = WPDB::Post.create(:post_title => 'Hello world', :post_author => 1)
6
- end
7
-
8
- it "creates a new post" do
9
- assert @post.ID
10
- end
11
-
12
- it "fetches all posts" do
13
- assert WPDB::Post.all
14
- end
15
-
16
- it "manages postmeta" do
17
- @post.add_postmeta(:meta_key => 'test', :meta_value => 'test')
18
- @post.save
19
-
20
- meta_value = @post.postmeta.first.meta_value
21
- assert_equal 'test', meta_value
22
- end
23
-
24
- it "manages the hierarchy of posts" do
25
- @post.add_child(WPDB::Post.create(:post_title => 'Child', :post_author => 1))
26
- @post.save
27
-
28
- assert_equal 'Child', @post.children.first.post_title
29
-
30
- @post.children.first.destroy
31
- end
32
-
33
- it "fetches revisions of posts" do
34
- revision = WPDB::Post.create(:post_type => 'revision', :post_title => 'Revision', :post_parent => @post.ID, :post_author => 1)
35
- assert_equal 'Revision', @post.revisions.first.post_title
36
-
37
- revision.destroy
38
- end
39
-
40
- it "fetches attachments to posts" do
41
- attachment = WPDB::Post.create(:post_type => 'attachment', :post_title => 'Attachment', :post_parent => @post.ID, :post_author => 1)
42
- assert_equal 'Attachment', @post.attachments.first.post_title
43
-
44
- attachment.destroy
45
- end
46
-
47
- after do
48
- @post.destroy
49
- end
50
- end
data/test/readme_test.rb DELETED
@@ -1,43 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- # Examples from the README; it's obviously important that they work
4
- # since they're people's first exposure to the project!
5
- describe "README" do
6
- it "performs more complex queries" do
7
- post = WPDB::Post.create(:post_title => 'aaaaa', :post_author => 1)
8
- post.add_postmeta(:meta_key => 'image', :meta_value => 'test')
9
- post.save
10
-
11
- meta_value = WPDB::Post.first(:post_title => /^[a-z]+$/, :ID => post.ID)
12
- .postmeta_dataset.first(:meta_key => 'image').meta_value
13
- assert_equal 'test', meta_value
14
-
15
- post.destroy
16
- end
17
-
18
- it "creates records" do
19
- post = WPDB::Post.create(:post_title => 'Test', :post_content => 'Testing, testing, 123', :post_author => 1)
20
- assert post.ID
21
- post.destroy
22
- end
23
-
24
- it "creates posts, users, and tags all in one go" do
25
- author = WPDB::User.create(
26
- :user_login => 'fred',
27
- :user_email => 'fred@example.com'
28
- )
29
-
30
- term = WPDB::Term.create(:name => 'Fred Stuff')
31
-
32
- post = WPDB::Post.create(
33
- :post_title => 'Hello from Fred',
34
- :post_content => 'Hello, world',
35
- :author => author
36
- )
37
- post.add_term(term, 'tag')
38
-
39
- author.destroy
40
- term.destroy
41
- post.destroy
42
- end
43
- end
data/test/terms_test.rb DELETED
@@ -1,42 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe WPDB::Term do
4
- before do
5
- @term = WPDB::Term.create(:name => 'test')
6
- @term_taxonomy = WPDB::TermTaxonomy.create(:term_id => @term.term_id, :taxonomy => 'category')
7
- end
8
-
9
- it "saves terms" do
10
- assert @term.term_id
11
- end
12
-
13
- it "attaches terms to posts" do
14
- post = WPDB::Post.create(:post_title => 'test', :post_author => 1)
15
- post.add_termtaxonomy(@term_taxonomy)
16
- post.save
17
-
18
- assert post.ID
19
- assert post.termtaxonomy.length
20
- assert_equal @term.term_id, post.termtaxonomy.first.term_id
21
- assert_equal 'category', post.termtaxonomy.first.taxonomy
22
-
23
- post.destroy
24
- end
25
-
26
- it "attaches terms to posts with the shorthand" do
27
- post = WPDB::Post.create(:post_title => 'test', :post_author => 1)
28
- post.add_term(@term, 'category')
29
- post.save
30
-
31
- assert post.ID
32
- assert post.termtaxonomy.length
33
- assert_equal @term.term_id, post.termtaxonomy.first.term_id
34
- assert_equal 'category', post.termtaxonomy.first.taxonomy
35
-
36
- post.destroy
37
- end
38
-
39
- after do
40
- @term.destroy
41
- end
42
- end
data/test/test_helper.rb DELETED
@@ -1,17 +0,0 @@
1
- $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
2
- require 'bundler'
3
- Bundler.setup
4
-
5
- require 'yaml'
6
- require 'letters'
7
-
8
- require 'sequel'
9
-
10
- require 'ruby-wpdb'
11
-
12
- WPDB.from_config
13
-
14
- require 'logger'
15
- WPDB.db.logger = Logger.new('data/query.log')
16
-
17
- require 'minitest/autorun'
data/test/users_test.rb DELETED
@@ -1,47 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe WPDB::User do
4
- before do
5
- @user = WPDB::User.create(
6
- :user_login => 'test',
7
- :user_pass => 'test',
8
- :user_nicename => 'Testy Testerson',
9
- :user_email => 'test@example.com',
10
- :user_registered => DateTime.now
11
- )
12
- end
13
-
14
- it "creates a new user" do
15
- assert @user.ID
16
- end
17
-
18
- it "adds usermeta" do
19
- @user.add_usermeta(:meta_key => 'test', :meta_value => 'test')
20
- @user.save
21
-
22
- user = WPDB::User.where(:ID => @user.ID).first
23
- meta_value = user.usermeta.first.meta_value
24
- assert_equal 'test', meta_value
25
- end
26
-
27
- it "hashes passwords" do
28
- @user.save
29
- assert_equal Digest::MD5.hexdigest('test'), @user.user_pass
30
- end
31
-
32
- it "registers the authorship of posts" do
33
- post = WPDB::Post.create(:post_title => "Testy's first post")
34
- @user.add_post(post)
35
- @user.reload
36
-
37
- assert_equal "Testy's first post", @user.posts.first.post_title
38
-
39
- assert_equal @user.ID, post.post_author
40
-
41
- post.destroy
42
- end
43
-
44
- after do
45
- @user.destroy
46
- end
47
- end