Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Now can create a basic post

  • Loading branch information...
commit 77632119cc66d9eb0010a6b8cfb18f355ab4b67e 1 parent 7cc598b
jipiboily authored
View
3  Gemfile
@@ -14,4 +14,5 @@ gem "jquery-rails"
# your gem to rubygems.org.
# To use debugger
-# gem 'ruby-debug'
+#gem 'ruby-debug'
+#gem 'ruby-debug19'
View
2  Gemfile.lock
@@ -74,6 +74,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.0.4)
+ mysql2 (0.3.11)
nokogiri (1.5.0)
polyglot (0.3.3)
rack (1.3.6)
@@ -146,5 +147,6 @@ DEPENDENCIES
guard-rspec
jquery-rails
monologue!
+ mysql2
rspec-rails (~> 2.8)
sqlite3
View
1  README.md
@@ -15,7 +15,6 @@ Somewhat basic mountable blogging engine in Rails built to be easily mounted in
- keywords
- categories
- content
- - excerpt
- an author (logged user)
- pages
- title
View
32 app/controllers/monologue/admin/posts_controller.rb
@@ -0,0 +1,32 @@
+class Monologue::Admin::PostsController < Monologue::Admin::BaseController
+ def index
+ end
+
+ def new
+ @post = Monologue::Post.new
+ @post.posts_revisions.build
+
+ end
+
+ def create
+ @post = Monologue::Post.new(params[:post])
+ @post.posts_revisions.first.user_id = current_user.id
+
+ respond_to do |format|
+ if @post.save
+ format.html { redirect_to ["admin", @post], :notice => 'Monologue created' }
+ format.json { render :json => ["admin", @post], :status => :created, :location => ["admin", @post] }
+ else
+ format.html { render :action => "new" }
+ format.json { render :json => @post.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ def edit
+ end
+
+ def update
+ render :edit
+ end
+end
View
10 app/controllers/monologue/admin/posts_revisions_controller.rb
@@ -1,10 +0,0 @@
-class Monologue::Admin::PostsRevisionsController < Monologue::Admin::BaseController
- def index
- end
-
- def new
- end
-
- def edit
- end
-end
View
59 app/form_builders/monologue_admin_form_builder.rb
@@ -0,0 +1,59 @@
+class MonologueAdminFormBuilder < ActionView::Helpers::FormBuilder
+ delegate :content_tag, :tag, to: :@template
+
+ %w[text_field text_area password_field collection_select select file_field].each do |method_name|
+ define_method(method_name) do |name, *args|
+ content_tag :div, class: "field" do
+ field_label(name, *args) + tag(:br) + super(name, *args)
+ end
+ end
+ end
+
+ def check_box(name, *args)
+ content_tag :div, class: "field" do
+ super + " " + field_label(name, *args)
+ end
+ end
+
+ def collection_check_boxes(attribute, records, record_id, record_name)
+ content_tag :div, class: "field" do
+ @template.hidden_field_tag("#{object_name}[#{attribute}][]") +
+ records.map do |record|
+ element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
+ checkbox = @template.check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute).include?(record.send(record_id)), id: element_id)
+ checkbox + " " + @template.label_tag(element_id, record.send(record_name))
+ end.join(tag(:br)).html_safe
+ end
+ end
+
+ def submit(*args)
+ content_tag :div, class: "actions" do
+ super
+ end
+ end
+
+ def error_messages
+ if object.errors.full_messages.any?
+ content_tag(:div, :class => "error_messages") do
+ content_tag(:h2, "Invalid Fields") +
+ content_tag(:ul) do
+ object.errors.messages.map do |msg|
+ content_tag(:li, msg[1][0])
+ end.join.html_safe
+ end
+ end
+ end
+ end
+
+ private
+
+ def field_label(name, *args)
+ options = args.extract_options!
+ required = object.class.validators_on(name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
+ label(name, options[:label], class: ("required" if required))
+ end
+
+ def objectify_options(options)
+ super.except(:label)
+ end
+end
View
4 app/helpers/monologue/application_helper.rb
@@ -1,4 +1,8 @@
module Monologue
module ApplicationHelper
+ def monologue_admin_form_for(object, options = {}, &block)
+ options[:builder] = MonologueAdminFormBuilder
+ form_for(object, options, &block)
+ end
end
end
View
1  app/models/monologue/post.rb
@@ -1,6 +1,7 @@
module Monologue
class Post < ActiveRecord::Base
has_many :posts_revisions
+ accepts_nested_attributes_for :posts_revisions
scope :published_revision, joins(:posts_revisions).where("posts_revision_id = monologue_posts_revisions.id")
validates :posts_revision_id, uniqueness: true
View
2  app/models/monologue/posts_revision.rb
@@ -6,7 +6,7 @@ class PostsRevision < ActiveRecord::Base
validates :content, presence: true
validates :url, presence: true
validates :user_id, presence: true
- validates :post_id, presence: true
+# validates :post_id, presence: true # TODO: do something about this validation on the first creation of a POST
validates :published_at, presence: true
end
View
1  app/views/monologue/admin/posts/edit.html.erb
@@ -0,0 +1 @@
+edit...
View
0  ...ws/monologue/admin/posts_revisions/index.html.erb → app/views/monologue/admin/posts/index.html.erb
File renamed without changes
View
13 app/views/monologue/admin/posts/new.html.erb
@@ -0,0 +1,13 @@
+<h1>New monologue</h1>
+<%= monologue_admin_form_for ["admin", @post] do |post| %>
+ <%= post.error_messages %>
+ <%= post.fields_for :posts_revisions do |revision| %>
+ <%= revision.text_field :title %>
+ <%= revision.text_area :content %>
+ <%= revision.text_field :url %>
+ <%= revision.text_field :published_at %>
+ <% end %>
+ <%= post.check_box :published %>
+
+ <%= post.submit :save %>
+<% end %>
View
1  app/views/monologue/admin/posts/show.html.erb
@@ -0,0 +1 @@
+show..
View
16 config/locales/en.yml
@@ -0,0 +1,16 @@
+en:
+ activerecord:
+ errors:
+ format: "%{message}"
+ errors:
+ full_messages: "%{message}"
+ errors:
+ models:
+ full_messages: "%{message}"
+ monologue/posts_revision:
+ blank:
+ "%{attribute} is required"
+ attributes:
+ published_at:
+ blank:
+ "'Published at' is required"
View
2  config/routes.rb
@@ -4,6 +4,6 @@
get "logout" => "sessions#destroy"
get "login" => "sessions#new"
resources :sessions
- resources :posts_revisions, path: "posts"
+ resources :posts
end
end
View
1  db/seeds.rb
@@ -0,0 +1 @@
+Monologue::User.create!({name: "Monologue", email: "monologue@example.com", password: "monologue", password_confirmation: "monologue"})
View
5 monologue.gemspec
@@ -24,6 +24,11 @@ Gem::Specification.new do |s|
s.add_development_dependency "capybara"
s.add_development_dependency "guard-rspec"
s.add_development_dependency "database_cleaner"
+ s.add_development_dependency "mysql2"
+
+ # s.add_development_dependency "linecache19"
+ # s.add_development_dependency "ruby-debug-base19"
+ # s.add_development_dependency 'ruby-debug19'
s.add_development_dependency "sqlite3"
View
2  spec/dummy/config/application.rb
@@ -34,6 +34,8 @@ class Application < Rails::Application
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
+
+ config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
View
28 spec/dummy/config/database.yml
@@ -1,22 +1,22 @@
-# SQLite version 3.x
-# gem install sqlite3
-#
-# Ensure the SQLite 3 gem is defined in your Gemfile
-# gem 'sqlite3'
development:
- adapter: sqlite3
- database: db/development.sqlite3
+ adapter: mysql2
+ encoding: utf8
+ reconnect: false
+ database: monologue_development
pool: 5
- timeout: 5000
+ username: root
+ password:
+ socket: /tmp/mysql.sock
-# Warning: The database defined as "test" will be erased and
-# re-generated from your development database when you run "rake".
-# Do not set this db to the same as development or production.
test:
- adapter: sqlite3
- database: db/test.sqlite3
+ adapter: mysql2
+ encoding: utf8
+ reconnect: false
+ database: monologue_test
pool: 5
- timeout: 5000
+ username: root
+ password:
+ socket: /tmp/mysql.sock
production:
adapter: sqlite3
View
4 spec/dummy/db/schema.rb
@@ -14,14 +14,12 @@
ActiveRecord::Schema.define(:version => 20120120193907) do
create_table "monologue_posts", :force => true do |t|
- t.string "url"
+ t.integer "posts_revision_id"
t.boolean "published"
t.datetime "created_at"
t.datetime "updated_at"
end
- add_index "monologue_posts", ["url"], :name => "index_monologue_posts_on_url", :unique => true
-
create_table "monologue_posts_revisions", :force => true do |t|
t.string "title"
t.text "content"
View
9 spec/factories/monologue_posts_and_revisions.rb
@@ -3,18 +3,19 @@
FactoryGirl.define do
factory :post, class: Monologue::Post do
- id 1
- url "/my/post/url"
+# id 1
published true
- posts_revision_id 1
+# posts_revision_id 1
+# association :posts_revisions, factory: :posts_revision
end
factory :posts_revision, class: Monologue::PostsRevision do
- sequence(:id) {|i| i }
+# sequence(:id) {|i| i }
title "my title"
content "this is some text with french accents éàöûù and so on...even html tags like <br />"
url "/my/url"
user_id 1
+# association :posts, factory: :post
post_id 1
published_at "2012-01-20 14:38:58"
end
View
3  spec/models/monologue/post_spec.rb
@@ -11,7 +11,8 @@
describe "validations" do
it "is not possible to have twice the same posts_revision_id" do
- expect { Factory(:post) }.to raise_error(ActiveRecord::RecordInvalid)
+ Factory(:post, posts_revision_id: 1)
+ expect { Factory(:post, posts_revision_id: 1) }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end
View
4 spec/models/monologue/posts_revision_spec.rb
@@ -23,9 +23,5 @@
@post.should_not be_valid
end
end
-
- it "is not possible to have twice the same posts_revision_id" do
- expect { Factory(:post) }.to raise_error(ActiveRecord::RecordInvalid)
- end
end
end
View
35 spec/requests/posts_spec.rb
@@ -1,30 +1,55 @@
+# encoding: UTF-8
require 'spec_helper'
describe "posts" do
context "logged in user" do
- before(:all) do
+ before(:each) do
log_in
end
it "can access post's admin" do
- visit admin_posts_revisions_path
+ visit admin_posts_path
page.should have_content "Add a monologue"
end
+
+ it "can create new post" do
+ visit new_admin_post_path
+ page.should have_content "New monologue"
+ fill_in "Title", with: "This is a monologue"
+ fill_in "Content", with: "C'est l'histoire d'un gars comprends tu...and finally it has some french accents àèùöûç...meh!"
+ fill_in "Url", with: "/2012/this-is-a-monologue"
+ fill_in "Published at", with: DateTime.now
+ click_button "save"
+ page.should have_content "Monologue created"
+ end
+
+ # it "adds a revision each time you save"
+ # end
+
+ it "will output error messages if error(s) there is" do
+ visit new_admin_post_path
+ page.should have_content "New monologue"
+ click_button "save"
+ page.should have_content "Title is required"
+ page.should have_content "Content is required"
+ page.should have_content "Url is required"
+ page.should have_content "'Published at' is required"
+ end
end
context "NOT logged in user" do
it "can NOT access post's admin" do
- visit admin_posts_revisions_path
+ visit admin_posts_path
page.should have_content "You must first log in"
end
it "can NOT create new post" do
- visit new_admin_posts_revision_path
+ visit new_admin_post_path
page.should have_content "You must first log in"
end
it "can NOT edit posts" do
pr = Factory(:posts_revision)
- visit edit_admin_posts_revision_path(pr)
+ visit edit_admin_post_path(pr)
page.should have_content "You must first log in"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.