Skip to content
Browse files

Added basic functionallity to get the project going

* Project setup
* Added sinatra and sinatra-authorization gems as submodules
* Added administration and public display of posts (announcements)
* Changed README file including notes for project setup
  • Loading branch information...
1 parent e981dfd commit 0090bae06a07bfb1e5c482ba868b9db230ec49e6 @dalibor dalibor committed
View
4 .gitignore
@@ -0,0 +1,4 @@
+*~
+*.db
+*.swp
+config/app.yml
View
6 .gitmodules
@@ -0,0 +1,6 @@
+[submodule "vendor/sinatra"]
+ path = vendor/sinatra
+ url = git://github.com/sinatra/sinatra.git
+[submodule "vendor/sinatra-authorization"]
+ path = vendor/sinatra-authorization
+ url = git@github.com:dalibor/sinatra-authorization.git
View
45 README.rdoc
@@ -3,6 +3,7 @@
* http://ruby.mk
* http://github.com/mkrug/ruby.mk
+
== DESCRIPTION:
Web site of Macedonian Ruby Users Group (MKRUG)
@@ -17,6 +18,50 @@ please write a private message and we will add you as a collaborator.
Then you can clone the repository and add your name in the License
copyright area and commit to check if everything works OK.
+
+== INSTALLATION
+
+- Install sinatra and sinatra-authorization gems as submodules
+
+ git submodule init
+ git submodule update
+
+- Install Data Mapper gems and database support
+
+ sudo gem install dm-core
+ sudo gem install dm-more
+
+ sudo apt-get install libsqlite3-dev # dependency for do_sqlite3
+ sudo gem install do_sqlite3 # data objects driver for SQLite3
+ sudo gem install do_mysql # data objects driver for MySQL
+
+- Setup database
+
+ Currently this project is setup to run on MySQL database:
+
+ mysql -u root -p # login to mysql console
+ CREATE DATABASE ruby_mk CHARACTER SET = utf8 # create ruby_mk database
+ DataMapper.setup(:default, 'mysql://root:pass@localhost/ruby_mk')
+
+ Then, go to irb and run:
+
+ irb
+ require 'lib/ruby_mk/models'
+ DataMapper.auto_migrate! # to create all tables or
+ DataMapper.auto_upgrade! # to create new added tables
+
+ For other database setups check out notes in lib/ruby_mk/models
+
+- Config application
+
+ cp config/app.yml.temp config/app.yml # copy and then edit app.yml with your credentials
+
+- Run application with thin server
+
+ thin start --rackup config.ru # run app with thin server
+ rackup config.ru -p 3000 # or with rackup
+
+
== LICENSE:
(The MIT License)
View
16 config.ru
@@ -0,0 +1,16 @@
+root_dir = File.dirname(__FILE__)
+$:.unshift File.join(root_dir, 'lib')
+Dir.entries('vendor').reject{|x| x == '.' || x == '..'}.each{|y| $:.unshift File.join(root_dir, 'vendor', y, 'lib') }
+
+require 'rubygems'
+require 'sinatra'
+require 'sinatra/authorization'
+require 'haml'
+require 'ruby_mk'
+
+$config = YAML.load_file(File.join(File.dirname(__FILE__), 'config', 'app.yml'))
+
+set :authorization_realm, "ruby.mk admin"
+set :views, File.join(root_dir, 'views')
+
+run Sinatra::Application
View
3 config/app.yml.temp
@@ -0,0 +1,3 @@
+credentials:
+ username: admin
+ password: password
View
7 lib/ruby_mk.rb
@@ -0,0 +1,7 @@
+#require 'ruby_mk/authorization'
+require 'ruby_mk/models'
+require 'ruby_mk/main'
+require 'ruby_mk/posts'
+require 'ruby_mk/admin/posts'
+require 'ruby_mk/helpers'
+
View
57 lib/ruby_mk/admin/posts.rb
@@ -0,0 +1,57 @@
+get "/admin/?" do
+ login_required
+ haml :"admin/index"
+end
+
+get "/admin/posts/?" do
+ login_required
+ @posts = Post.all
+ haml :"admin/posts/index"
+end
+
+get "/admin/posts/new" do
+ login_required
+ @post = Post.new
+ haml :"admin/posts/new"
+end
+
+post "/admin/posts" do
+ login_required
+ @post = Post.new(params[:post])
+ if @post.save
+ redirect "/posts/#{@post.permalink}"
+ else
+ haml :"admin/posts/new"
+ end
+end
+
+get "/admin/posts/:permalink/edit/?" do |permalink|
+ login_required
+ @post = Post.first(:permalink => permalink)
+ haml :"admin/posts/edit"
+end
+
+put "/admin/posts/:permalink" do |permalink|
+ login_required
+ @post = Post.first(:permalink => permalink)
+ permalink_was = @post.permalink
+ if @post.update_attributes(params[:post])
+ redirect "/posts/#{@post.permalink}"
+ else
+ @post.permalink = permalink_was # hack to ensure that permalink is not changed when record not saved
+ haml :"admin/posts/edit"
+ end
+end
+
+delete "/admin/posts/:permalink" do |permalink|
+ login_required
+ @post = Post.first(:permalink => permalink)
+ @post.destroy
+ redirect "/admin/posts"
+end
+
+get "/admin/posts/:permalink/delete/?" do |permalink|
+ login_required
+ @post = Post.first(:permalink => permalink)
+ haml :"admin/posts/delete"
+end
View
18 lib/ruby_mk/helpers.rb
@@ -0,0 +1,18 @@
+helpers do
+
+ def authorize(login, password)
+ login == $config['credentials']['username'] && password == $config['credentials']['password']
+ end
+
+ def post_path(post)
+ "/posts/#{post.permalink}"
+ end
+
+ def page_title
+ if @post && !@post.new?
+ "#{@post.title} - ruby.mk"
+ else
+ "ruby.mk"
+ end
+ end
+end
View
7 lib/ruby_mk/main.rb
@@ -0,0 +1,7 @@
+# index
+get "/" do
+ @posts = Post.all
+ haml :"index"
+end
+
+
View
54 lib/ruby_mk/models.rb
@@ -0,0 +1,54 @@
+require 'rubygems'
+require 'dm-core'
+require 'dm-aggregates'
+require 'dm-validations'
+require 'dm-timestamps'
+
+# Specify your database connection before you define your models
+
+# SQLite connection
+#DataMapper.setup(:default, "sqlite3:ruby_mk.db")
+
+# A MySQL connection:
+# mysql -u root -p # login to mysql console
+# CREATE DATABASE ruby_mk CHARACTER SET = utf8 # create ruby_mk database
+DataMapper.setup(:default, 'mysql://root:pass@localhost/ruby_mk')
+
+# A Postgres connection:
+# DataMapper.setup(:default, 'postgres://localhost/ruby_mk')
+
+class Post
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :title, String, :nullable => false
+ property :permalink, String, :nullable => false
+ property :content, Text, :nullable => false
+
+
+ #validates_present :title, :content
+ before :valid?, :set_permalink
+
+ has n, :comments # has_many :comments
+
+ private
+ def set_permalink
+ self.permalink = title.gsub(/\s+/, "-").downcase if title
+ end
+
+end
+
+class Comment
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :post_id, Integer, :nullable => false
+ property :author, String, :nullable => false
+ property :email, String, :nullable => false
+ property :url, String, :nullable => false
+ property :content, Text, :nullable => false
+
+ #validates_present :post_id, :author, :email, :url, :content
+
+ belongs_to :post
+end
View
20 lib/ruby_mk/posts.rb
@@ -0,0 +1,20 @@
+get "/posts" do
+ @posts = Post.all
+ erb :index
+end
+
+get "/posts/:permalink" do |permalink|
+ @post = Post.first(:permalink => permalink)
+ @comment = Comment.new
+ haml :show
+end
+
+post "/posts/:permalink/comments" do |permalink|
+ @post = Post.first(:permalink => permalink)
+ @comment = @post.comments.build(params[:comment])
+ if @comment.save
+ redirect post_path(@post)
+ else
+ haml :show
+ end
+end
View
3 public/css/style.css
@@ -0,0 +1,3 @@
+.admin {
+ float: right;
+}
View
15 public/js/app.js
@@ -0,0 +1,15 @@
+function destroy(that) {
+ if (confirm('Are you sure?')) {
+ var f = document.createElement('form');
+ f.style.display = 'none';
+ that.parentNode.appendChild(f);
+ f.method = 'POST';
+ f.action = that.href.match(/(.*)\/delete/)[1];
+ var m = document.createElement('input');
+ m.setAttribute('type', 'hidden');
+ m.setAttribute('name', '_method');
+ m.setAttribute('value', 'delete');
+ f.appendChild(m);
+ f.submit();
+ };
+}
1 vendor/sinatra
@@ -0,0 +1 @@
+Subproject commit 7ef1af585784d881678c73185b194cbc63d1826f
1 vendor/sinatra-authorization
@@ -0,0 +1 @@
+Subproject commit a51a6fff67bf93be9a3d281507153ff8abd820c6
View
3 views/admin/index.haml
@@ -0,0 +1,3 @@
+%h1 ruby.mk admin panel
+%a{:href => "/admin/posts"}
+ Posts
View
10 views/admin/posts/delete.haml
@@ -0,0 +1,10 @@
+%p
+ Are you sure you want to destroy "#{@post.title}"
+%form{ :method => "post", :action => "/admin/posts/#{@post.permalink}" }
+ %input{ :name => "_method", :value => "delete", :type => "hidden" }
+ %div
+ %button{ :type => "submit" }
+ Destroy
+ or go
+ %a{ :href => "/admin/posts"}
+ back
View
27 views/admin/posts/edit.haml
@@ -0,0 +1,27 @@
+- unless @post.errors.empty?
+ %h4
+ Errors:
+ %ul
+ - @post.errors.full_messages.each do |message|
+ %li
+ = message
+%form{ :method => "post", :action => "/admin/posts/#{@post.permalink}" }
+ %div
+ %input{ :name => "_method", :type => "hidden", :value => "put" }
+ %label{ :for => "post_title" }
+ Title
+ %br
+ %input#post_title{ :name => "post[title]", :type => "text", :value => @post.title }
+ %div
+ %label{ :for => "post_body" }
+ Your Article
+ %br
+ %textarea#post_body{ :name => "post[content]", :rows => "20", :cols => "40" }
+ = @post.content
+ %div
+ %button{ :type => "submit" }
+ Save
+ or
+ %a{ :href => "#{post_path(@post)}" }
+ go back
+ \.
View
21 views/admin/posts/index.haml
@@ -0,0 +1,21 @@
+%h1
+ Admin posts
+%p
+ %a{:href => "/admin/posts/new"}
+ New post
+- if @posts.empty?
+ No posts.
+- else
+ %ul#posts
+ - for post in @posts
+ %li.post
+ %h2
+ %a{ :href => "/posts/#{post.permalink}" }
+ = post.title
+ = post.content
+ %br
+ %a{ :href => "/admin/posts/#{post.permalink}/edit" }
+ edit
+  |
+ %a{ :href => "/admin/posts/#{post.permalink}/delete", :onclick => "destroy(this); return false;" }
+ destroy
View
26 views/admin/posts/new.haml
@@ -0,0 +1,26 @@
+- unless @post.errors.empty?
+ %h4
+ Errors:
+ %ul
+ - @post.errors.full_messages.each do |message|
+ %li
+ = message
+%form{ :method => "post", :action => "/admin/posts" }
+ %div
+ %label{ :for => "post_title" }
+ Title
+ %br
+ %input#post_title{ :name => "post[title]", :type => "text", :value => @post.title }
+ %div
+ %label{ :for => "post_body" }
+ Content
+ %br
+ %textarea#post_body{ :name => "post[content]", :rows => "20", :cols => "40" }
+ = @post.content
+ %div
+ %button{ :type => "submit" }
+ Save
+ or
+ %a{ :href => "/#{@post.permalink}" }
+ go back
+ \.
View
9 views/index.haml
@@ -0,0 +1,9 @@
+%h1
+ "No news ia good news"
+%ul#posts
+ - for post in @posts
+ %li.post
+ %h2
+ %a{ :href => "posts/#{post.permalink}" }
+ = post.title
+ = post.content
View
14 views/layout.haml
@@ -0,0 +1,14 @@
+!!!
+%html{ :lang => "en" }
+ %head
+ %title
+ = page_title
+ %link{ :href => "/css/style.css", :rel => "stylesheet", :type => "text/css" }
+ %script{ :type => "text/javascript", :src => "/js/app.js" }
+ %body
+ <a href="/">ruby.mk</a>
+ #header
+ .admin
+ %a{ :href => "/admin" }
+ admin
+ = yield
View
46 views/post.haml
@@ -0,0 +1,46 @@
+%h2
+ %a{ :href => post_path(@post) }
+ = @post.title
+= @post.content
+#comments
+ %ul
+ - for comment in @post.comments
+ %li
+ %p
+ = escape_html comment.content
+ %address
+ \&mdash;
+ = escape_html comment.author
+ - unless @comment.errors.empty?
+ %h4
+ Errors:
+ %ul
+ - @comment.errors.full_messages.each do |message|
+ %li
+ = message
+ %form{ :method => "post", :action => "#{post_path(@post)}/comments" }
+ %div
+ %label{ :for => "comment_author" }
+ Name
+ %br
+ %input#comment_author{ :name => "comment[author]", :type => "text", :value => @comment.author }
+ %div
+ %label{ :for => "comment_email" }
+ Email
+ %br
+ %input#comment_email{ :name => "comment[email]", :type => "text", :value => @comment.email }
+ %div
+ %label{ :for => "comment_url" }
+ URL
+ %br
+ %input#comment_url{ :name => "comment[url]", :type => "text", :value => @comment.url }
+ %div
+ %label{ :for => "comment_content" }
+ Your Comment
+ %br
+ %textarea#comment_content{ :name => "comment[content]", :rows => "6", :cols => "40" }
+ = @comment.content
+ %div
+ %button{ :type => "submit" }
+ Leave Comment
+
View
1 views/show.haml
@@ -0,0 +1 @@
+= haml(:post, :layout => false, :locals => { :post => @post })

0 comments on commit 0090bae

Please sign in to comment.
Something went wrong with that request. Please try again.