rails_wordpress 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Guardfile +77 -0
- data/LICENSE +22 -0
- data/Rakefile +28 -0
- data/app/assets/javascripts/wordpress/application.js +13 -0
- data/app/assets/stylesheets/wordpress/application.css +15 -0
- data/app/controllers/wordpress/application_controller.rb +4 -0
- data/app/helpers/wordpress/application_helper.rb +4 -0
- data/app/models/wordpress/attachment.rb +7 -0
- data/app/models/wordpress/category.rb +18 -0
- data/app/models/wordpress/link_category.rb +4 -0
- data/app/models/wordpress/page.rb +4 -0
- data/app/models/wordpress/post.rb +4 -0
- data/app/models/wordpress/post_tag.rb +14 -0
- data/app/models/wordpress/relationship.rb +26 -0
- data/app/models/wordpress/revision.rb +4 -0
- data/app/models/wordpress/taxonomy.rb +56 -0
- data/app/models/wordpress/term.rb +19 -0
- data/app/models/wordpress/user.rb +7 -0
- data/app/models/wordpress/wp_post.rb +195 -0
- data/app/views/layouts/wordpress/application.html.erb +14 -0
- data/config/routes.rb +2 -0
- data/lib/rails_wordpress/engine.rb +18 -0
- data/lib/rails_wordpress/version.rb +3 -0
- data/lib/rails_wordpress.rb +4 -0
- data/lib/tasks/word_press_tasks.rake +4 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config/application.rb +26 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +24 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/schema.rb +164 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +636 -0
- data/spec/dummy/log/test.log +6427 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/factories/attachment_factory.rb +11 -0
- data/spec/factories/category_factory.rb +9 -0
- data/spec/factories/link_category_factory.rb +6 -0
- data/spec/factories/page_factory.rb +11 -0
- data/spec/factories/post_factory.rb +19 -0
- data/spec/factories/post_tag_factory.rb +9 -0
- data/spec/factories/relationship_factory.rb +4 -0
- data/spec/factories/revision_factory.rb +12 -0
- data/spec/factories/taxonomy_factory.rb +6 -0
- data/spec/factories/term_factory.rb +5 -0
- data/spec/factories/user_factory.rb +6 -0
- data/spec/factories/wp_post_factory.rb +11 -0
- data/spec/models/wordpress/attachment_spec.rb +7 -0
- data/spec/models/wordpress/category_spec.rb +32 -0
- data/spec/models/wordpress/link_category_spec.rb +7 -0
- data/spec/models/wordpress/page_spec.rb +31 -0
- data/spec/models/wordpress/post_spec.rb +88 -0
- data/spec/models/wordpress/post_tag_spec.rb +21 -0
- data/spec/models/wordpress/relationship_spec.rb +38 -0
- data/spec/models/wordpress/revision_spec.rb +87 -0
- data/spec/models/wordpress/taxonomy_spec.rb +13 -0
- data/spec/models/wordpress/term_spec.rb +11 -0
- data/spec/models/wordpress/user_spec.rb +25 -0
- data/spec/models/wordpress/wp_post_spec.rb +9 -0
- data/spec/rails_helper.rb +15 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/support/database_cleaner.rb +25 -0
- metadata +313 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c6da637feb5804bb2dd8a686287d72418c3cff85
|
4
|
+
data.tar.gz: c549878b5d0dcc175931be46d8958e0b0b08317a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b2a8023b0ea11943ab9a09dbce0ed36895085a687b4203653a3edba5ba378c2a1db89e52734cd826d3c61aeeef6c37ff081dc8647652e5b9337cd4f075eb780a
|
7
|
+
data.tar.gz: 4bee3cf8db5f641e11155e582d1ac183ab9fd2de990e981c6ee525cb1f3ac846a9178183dff9d7babf6b1661b0e954213eccbac0aa7b26dd23b506aefe5177ad
|
data/Guardfile
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features)
|
6
|
+
|
7
|
+
## Uncomment to clear the screen before every task
|
8
|
+
# clearing :on
|
9
|
+
|
10
|
+
## Guard internally checks for changes in the Guardfile and exits.
|
11
|
+
## If you want Guard to automatically start up again, run guard in a
|
12
|
+
## shell loop, e.g.:
|
13
|
+
##
|
14
|
+
## $ while bundle exec guard; do echo "Restarting Guard..."; done
|
15
|
+
##
|
16
|
+
## Note: if you are using the `directories` clause above and you are not
|
17
|
+
## watching the project directory ('.'), then you will want to move
|
18
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
19
|
+
#
|
20
|
+
# $ mkdir config
|
21
|
+
# $ mv Guardfile config/
|
22
|
+
# $ ln -s config/Guardfile .
|
23
|
+
#
|
24
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
25
|
+
|
26
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
27
|
+
# rspec may be run, below are examples of the most common uses.
|
28
|
+
# * bundler: 'bundle exec rspec'
|
29
|
+
# * bundler binstubs: 'bin/rspec'
|
30
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
31
|
+
# installed the spring binstubs per the docs)
|
32
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
33
|
+
# * 'just' rspec: 'rspec'
|
34
|
+
|
35
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
36
|
+
require "guard/rspec/dsl"
|
37
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
38
|
+
|
39
|
+
# Feel free to open issues for suggestions and improvements
|
40
|
+
|
41
|
+
# RSpec files
|
42
|
+
rspec = dsl.rspec
|
43
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
44
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
45
|
+
watch(rspec.spec_files)
|
46
|
+
|
47
|
+
# Ruby files
|
48
|
+
ruby = dsl.ruby
|
49
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
50
|
+
|
51
|
+
# Rails files
|
52
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
53
|
+
dsl.watch_spec_files_for(rails.app_files)
|
54
|
+
dsl.watch_spec_files_for(rails.views)
|
55
|
+
|
56
|
+
watch(rails.controllers) do |m|
|
57
|
+
[
|
58
|
+
rspec.spec.("routing/#{m[1]}_routing"),
|
59
|
+
rspec.spec.("controllers/#{m[1]}_controller"),
|
60
|
+
rspec.spec.("acceptance/#{m[1]}")
|
61
|
+
]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Rails config changes
|
65
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
66
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
67
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
68
|
+
|
69
|
+
# Capybara features specs
|
70
|
+
watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
|
71
|
+
|
72
|
+
# Turnip features and steps
|
73
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
74
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
75
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
76
|
+
end
|
77
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Michael Lang
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
require 'rspec/core'
|
9
|
+
require 'rspec/core/rake_task'
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'Wordpress'
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc.rdoc_files.include('README.rdoc')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
+
end
|
18
|
+
|
19
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
20
|
+
load 'rails/tasks/engine.rake'
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
|
24
|
+
|
25
|
+
desc "Run all specs in spec directory (excluding plugin specs)"
|
26
|
+
RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
|
27
|
+
|
28
|
+
task :default => :spec
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Wordpress
|
2
|
+
class Category < Taxonomy
|
3
|
+
has_many :sub_categories, class_name: "Wordpress::Category", foreign_key: :parent, primary_key: :id
|
4
|
+
|
5
|
+
def self.cloud
|
6
|
+
cats = all.reject{|r| r.posts.empty?}
|
7
|
+
total_posts = cats.inject(0){|sum, t| sum += t.count}
|
8
|
+
cats.map{|t| {category: t, size: 1.0 + (t.count / total_posts.to_f * 2)}}.sort_by{|sb| sb[:category].slug}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.find_or_create category_name, parent = nil
|
12
|
+
raise "category name can't be blank" if category_name.blank?
|
13
|
+
parent_id = parent.try(:id).to_i
|
14
|
+
category = joins(:term).where(wp_terms: {name: category_name}, parent: parent_id).first
|
15
|
+
category ||= create!(description: category_name, term_id: Term.create!(name: category_name).id, parent: parent_id)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Wordpress
|
2
|
+
class PostTag < Taxonomy
|
3
|
+
def self.cloud
|
4
|
+
tags_for_cloud = for_cloud.all
|
5
|
+
total_tags = tags_for_cloud.inject(0){|sum, t| sum += t.count}
|
6
|
+
tags_for_cloud.map{|t| {tag: t, size: 1.0 + (t.count / total_tags.to_f * 5)}}.sort_by{|sb| sb[:tag].slug}
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.find_or_create tag_name
|
10
|
+
raise "tag name can't be blank" if tag_name.blank?
|
11
|
+
joins(:term).where(wp_terms: {name: tag_name}).first || create!(term_id: Term.create!(name: tag_name).id)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# +------------------+---------------------+------+-----+---------+-------+
|
2
|
+
# | Field | Type | Null | Key | Default | Extra |
|
3
|
+
# +------------------+---------------------+------+-----+---------+-------+
|
4
|
+
# | object_id | bigint(20) unsigned | NO | PRI | 0 | |
|
5
|
+
# | term_taxonomy_id | bigint(20) unsigned | NO | PRI | 0 | |
|
6
|
+
# | term_order | int(11) | NO | | 0 | |
|
7
|
+
# +------------------+---------------------+------+-----+---------+-------+
|
8
|
+
module Wordpress
|
9
|
+
class Relationship < ActiveRecord::Base
|
10
|
+
self.table_name = "wp_term_relationships"
|
11
|
+
self.primary_key = nil
|
12
|
+
after_save :increment_term_use_count
|
13
|
+
before_destroy :decrement_term_use_count
|
14
|
+
|
15
|
+
belongs_to :post, class_name: "Wordpress::Post", foreign_key: "object_id"
|
16
|
+
belongs_to :taxonomy, class_name: "Wordpress::Taxonomy", foreign_key: "term_taxonomy_id"
|
17
|
+
|
18
|
+
def increment_term_use_count
|
19
|
+
self.taxonomy.update_attribute(:count, self.taxonomy.count + 1) if self.taxonomy.present?
|
20
|
+
end
|
21
|
+
|
22
|
+
def decrement_term_use_count
|
23
|
+
self.taxonomy.update_attribute(:count, self.taxonomy.count - 1) if self.taxonomy.present?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# +------------------+---------------------+------+-----+---------+----------------+
|
2
|
+
# | Field | Type | Null | Key | Default | Extra |
|
3
|
+
# +------------------+---------------------+------+-----+---------+----------------+
|
4
|
+
# | term_taxonomy_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
|
5
|
+
# | term_id | bigint(20) unsigned | NO | MUL | 0 | |
|
6
|
+
# | taxonomy | varchar(32) | NO | MUL | | |
|
7
|
+
# | description | longtext | NO | | NULL | |
|
8
|
+
# | parent | bigint(20) unsigned | NO | | 0 | |
|
9
|
+
# | count | bigint(20) | NO | | 0 | |
|
10
|
+
# +------------------+---------------------+------+-----+---------+----------------+
|
11
|
+
module Wordpress
|
12
|
+
class Taxonomy < ActiveRecord::Base
|
13
|
+
self.table_name = "wp_term_taxonomy"
|
14
|
+
self.primary_key = "term_taxonomy_id"
|
15
|
+
self.inheritance_column = 'taxonomy'
|
16
|
+
|
17
|
+
def self.find_sti_class type_name
|
18
|
+
"wordpress/#{type_name}".camelize.constantize
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.sti_name
|
22
|
+
name.underscore.split("/").last
|
23
|
+
end
|
24
|
+
|
25
|
+
before_create :set_defaults
|
26
|
+
|
27
|
+
has_many :relationships, foreign_key: "term_taxonomy_id"
|
28
|
+
has_many :posts, through: :relationships
|
29
|
+
has_many :categories, through: :relationships
|
30
|
+
has_many :tags, through: :relationships
|
31
|
+
|
32
|
+
has_one :parent_node,
|
33
|
+
class_name: "Taxonomy",
|
34
|
+
primary_key: :parent,
|
35
|
+
foreign_key: :term_taxonomy_id
|
36
|
+
|
37
|
+
has_one :term, foreign_key: "term_id", primary_key: :term_id
|
38
|
+
|
39
|
+
scope :for_cloud, -> { includes(:term).order("count desc").limit(40) }
|
40
|
+
|
41
|
+
delegate :name, :to => :term, :allow_nil => true
|
42
|
+
delegate :slug, :to => :term, :allow_nil => true
|
43
|
+
|
44
|
+
def set_defaults
|
45
|
+
self.description = '' unless self.description_changed?
|
46
|
+
end
|
47
|
+
|
48
|
+
def breadcrumbs
|
49
|
+
(parent_node ? [parent_node.breadcrumbs, self] : [self]).flatten
|
50
|
+
end
|
51
|
+
|
52
|
+
def title
|
53
|
+
[name, description].compact.join(": ")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# +------------+---------------------+------+-----+---------+----------------+
|
2
|
+
# | Field | Type | Null | Key | Default | Extra |
|
3
|
+
# +------------+---------------------+------+-----+---------+----------------+
|
4
|
+
# | term_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
|
5
|
+
# | name | varchar(200) | NO | MUL | | |
|
6
|
+
# | slug | varchar(200) | NO | UNI | | |
|
7
|
+
# | term_group | bigint(10) | NO | | 0 | |
|
8
|
+
# +------------+---------------------+------+-----+---------+----------------+
|
9
|
+
module Wordpress
|
10
|
+
class Term < ActiveRecord::Base
|
11
|
+
self.table_name = "wp_terms"
|
12
|
+
before_save :set_slug_value
|
13
|
+
validates :slug, uniqueness: true
|
14
|
+
|
15
|
+
def set_slug_value
|
16
|
+
self.slug = self.name.parameterize
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,195 @@
|
|
1
|
+
module Wordpress
|
2
|
+
class WpPost < ActiveRecord::Base
|
3
|
+
self.table_name = 'wp_posts'
|
4
|
+
self.inheritance_column = 'post_type'
|
5
|
+
|
6
|
+
before_create :set_defaults
|
7
|
+
before_save :touch_values
|
8
|
+
after_save :save_relationships
|
9
|
+
|
10
|
+
scope :published, -> { where(post_status: "publish") }
|
11
|
+
scope :descending, -> { order(post_modified: :desc, id: :desc) }
|
12
|
+
scope :recent, -> (count = 10) { descending.limit(count) }
|
13
|
+
|
14
|
+
belongs_to :parent, class_name: "Post", foreign_key: "post_parent"
|
15
|
+
|
16
|
+
has_many :relationships, foreign_key: "object_id"
|
17
|
+
has_many :tags, class_name: "PostTag", through: :relationships, source: :taxonomy, :dependent => :destroy
|
18
|
+
has_many :categories, class_name: "Category", through: :relationships, source: :taxonomy, :dependent => :destroy
|
19
|
+
|
20
|
+
belongs_to :author, class_name: "User", foreign_key: "post_author"
|
21
|
+
|
22
|
+
def self.find_sti_class type_name
|
23
|
+
"wordpress/#{type_name}".camelize.constantize
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.sti_name
|
27
|
+
name.underscore.split("/").last
|
28
|
+
end
|
29
|
+
|
30
|
+
has_many :revisions, class_name: "Revision", foreign_key: "post_parent"
|
31
|
+
|
32
|
+
def new_revision params = {}
|
33
|
+
result = self.revisions.new \
|
34
|
+
post_author: self.post_author,
|
35
|
+
post_date: self.post_date,
|
36
|
+
post_date_gmt: self.post_date_gmt,
|
37
|
+
post_content: (params[:post_content] || self.content),
|
38
|
+
post_title: (params[:post_title] || self.title),
|
39
|
+
post_excerpt: (params[:post_excerpt] || self.excerpt),
|
40
|
+
post_status: 'inherit',
|
41
|
+
ping_status: (self.ping_status || 'closed'),
|
42
|
+
post_password: '',
|
43
|
+
post_name: "#{self.id}-revision-v#{self.revisions.count + 1}",
|
44
|
+
to_ping: self.to_ping,
|
45
|
+
pinged: self.to_ping,
|
46
|
+
post_modified: Time.now,
|
47
|
+
post_modified_gmt: Time.now.utc,
|
48
|
+
post_content_filtered: '',
|
49
|
+
guid: SecureRandom.uuid,
|
50
|
+
menu_order: self.menu_order,
|
51
|
+
post_mime_type: '',
|
52
|
+
comment_count: 0
|
53
|
+
|
54
|
+
result.post_tags = params[:post_tags] if params[:post_tags]
|
55
|
+
result.post_categories = params[:post_categories] if params[:post_categories]
|
56
|
+
result
|
57
|
+
end
|
58
|
+
|
59
|
+
def latest_revision
|
60
|
+
revisions.descending.first || self
|
61
|
+
end
|
62
|
+
|
63
|
+
def title
|
64
|
+
latest_revision.post_title
|
65
|
+
end
|
66
|
+
|
67
|
+
def content
|
68
|
+
latest_revision.post_content
|
69
|
+
end
|
70
|
+
|
71
|
+
def excerpt
|
72
|
+
latest_revision.post_excerpt
|
73
|
+
end
|
74
|
+
|
75
|
+
def created_at
|
76
|
+
self.post_date
|
77
|
+
end
|
78
|
+
|
79
|
+
def updated_at
|
80
|
+
latest_revision.post_modified
|
81
|
+
end
|
82
|
+
|
83
|
+
def set_defaults
|
84
|
+
p = self.parent
|
85
|
+
self.author = self.parent.try(:author) || User.first
|
86
|
+
|
87
|
+
self.post_date = (self.parent.try(:post_date) || Time.now) \
|
88
|
+
unless self.post_date_changed?
|
89
|
+
|
90
|
+
self.post_date_gmt = (self.parent.try(:post_date_gmt) || self.post_date.utc) \
|
91
|
+
unless self.post_date_gmt_changed?
|
92
|
+
|
93
|
+
content = (self.post_content || self.parent.try(:post_content)).to_s
|
94
|
+
self.post_excerpt = content.length >= 512 ? "#{content.slice(0, 512)}..." : content \
|
95
|
+
unless self.post_content_changed?
|
96
|
+
|
97
|
+
self.to_ping = '' unless self.to_ping_changed?
|
98
|
+
self.pinged = '' unless self.pinged_changed?
|
99
|
+
self.post_content_filtered = '' unless self.post_content_filtered_changed?
|
100
|
+
end
|
101
|
+
|
102
|
+
def first_revision
|
103
|
+
self.parent || self
|
104
|
+
end
|
105
|
+
|
106
|
+
def touch_values
|
107
|
+
self.post_modified = Time.now unless self.post_modified_changed?
|
108
|
+
self.post_modified_gmt = self.post_modified.utc unless self.post_modified_gmt_changed?
|
109
|
+
self.post_name = self.post_title.parameterize unless self.post_name_changed?
|
110
|
+
end
|
111
|
+
|
112
|
+
def save_relationships
|
113
|
+
return if self.first_revision == self
|
114
|
+
self.first_revision.tags.each{|item| item.destroy if item.marked_for_destruction? }
|
115
|
+
self.first_revision.categories.each{|item| item.destroy if item.marked_for_destruction? }
|
116
|
+
self.first_revision.save
|
117
|
+
end
|
118
|
+
|
119
|
+
def tag_names
|
120
|
+
first_revision.tags.map(&:name)
|
121
|
+
end
|
122
|
+
|
123
|
+
def tag_slugs
|
124
|
+
first_revision.tags.map(&:slug)
|
125
|
+
end
|
126
|
+
|
127
|
+
def category_names
|
128
|
+
first_revision.categories.map(&:name)
|
129
|
+
end
|
130
|
+
|
131
|
+
def category_slugs
|
132
|
+
first_revision.categories.map(&:slug)
|
133
|
+
end
|
134
|
+
|
135
|
+
def created_at
|
136
|
+
first_revision.post_date
|
137
|
+
end
|
138
|
+
|
139
|
+
def has_category? category
|
140
|
+
if category.is_a?(String)
|
141
|
+
first_revision.categories.map(&:name).include? category
|
142
|
+
else
|
143
|
+
first_revision.categories.include? category
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def post_tags
|
148
|
+
first_revision.tags.map(&:name).join(",")
|
149
|
+
end
|
150
|
+
|
151
|
+
def post_tags= tag_names
|
152
|
+
tag_names = tag_names.split(",").map{ |name| name.strip } unless tag_names.is_a?(Array)
|
153
|
+
tags_to_assign = tag_names.map{ |tag_name| PostTag.find_or_create(tag_name) }
|
154
|
+
new_tags = tags_to_assign.reject{ |tag| first_revision.tags.include?(tag) }
|
155
|
+
|
156
|
+
first_revision.tags.each{ |tag| tag.mark_for_destruction unless tags_to_assign.include?(tag) }
|
157
|
+
new_tags.each{ |tag| first_revision.relationships.build(taxonomy: tag) }
|
158
|
+
end
|
159
|
+
|
160
|
+
def post_categories
|
161
|
+
first_revision.categories.map(&:name).join(",")
|
162
|
+
end
|
163
|
+
|
164
|
+
def assign_category_ids category_ids
|
165
|
+
categories = category_ids.map{ |id| Category.find(id) rescue nil }.compact
|
166
|
+
new_categories = categories.reject{ |category| first_revision.categories.include?(category) }
|
167
|
+
|
168
|
+
first_revision.categories.each{ |cat| cat.mark_for_destruction unless categories.include?(cat) }
|
169
|
+
new_categories.each do |category|
|
170
|
+
first_revision.relationships.build(taxonomy: category)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def assign_category_names category_names
|
175
|
+
category_ids = category_names.map{ |name| Category.find_or_create(name).id }.compact
|
176
|
+
assign_category_ids category_ids
|
177
|
+
end
|
178
|
+
|
179
|
+
def post_categories= category_list
|
180
|
+
# turn into an array of items unless already so
|
181
|
+
unless category_list.is_a?(Array)
|
182
|
+
category_list = category_list.to_s.split(",")
|
183
|
+
.map{ |name| name.strip }
|
184
|
+
.reject{|r| r.blank?}
|
185
|
+
end
|
186
|
+
|
187
|
+
# its an array of ids when #to_i => #to_s yields the same
|
188
|
+
if category_list.map(&:to_i).map(&:to_s) == category_list
|
189
|
+
assign_category_ids category_list
|
190
|
+
else
|
191
|
+
assign_category_names category_list
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Wordpress</title>
|
5
|
+
<%= stylesheet_link_tag "wordpress/application", media: "all" %>
|
6
|
+
<%= javascript_include_tag "wordpress/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Wordpress
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace Wordpress
|
4
|
+
|
5
|
+
config.generators do |g|
|
6
|
+
g.test_framework :rspec, :fixture => false
|
7
|
+
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
8
|
+
g.assets false
|
9
|
+
g.helper false
|
10
|
+
end
|
11
|
+
|
12
|
+
config.to_prepare do
|
13
|
+
Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
|
14
|
+
require_dependency(c)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|