Permalink
Browse files

Events basics

  • Loading branch information...
1 parent 00fea80 commit 96986b44787b55a5caab2270a2a6977fd42932a0 @juanignaciosl juanignaciosl committed Apr 23, 2012
@@ -0,0 +1,93 @@
+# -*- encoding: utf-8 -*-
+
+class Backend::EventsController < BackendController
+ before_filter :find_event, except: [:index, :new, :create]
+
+ def find_event
+ @event = Event.find(params[:id])
+ end
+ protected :find_event
+
+ # GET /events
+ # GET /events.json
+ def index
+ @events = Event.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @events }
+ end
+ end
+
+ # GET /events/1
+ # GET /events/1.json
+ def show
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @event }
+ end
+ end
+
+ # GET /events/new
+ # GET /events/new.json
+ def new
+ @event = Event.new
+ @event.default_images
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @event }
+ end
+ end
+
+ # GET /events/1/edit
+ def edit
+ @event.default_images
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @event }
+ end
+ end
+
+ # POST /events
+ # POST /events.json
+ def create
+ @event = Event.new(params[:event])
+
+ respond_to do |format|
+ if @event.save
+ format.html { redirect_to backend_event_url(@event), notice: 'Event was successfully created.' }
+ format.json { render json: @event, status: :created, location: @event }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @event.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /events/1
+ # PUT /events/1.json
+ def update
+ respond_to do |format|
+ if @event.update_attributes(params[:event])
+ format.html { redirect_to backend_event_url(@event), notice: 'Event was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @event.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /events/1
+ # DELETE /events/1.json
+ def destroy
+ @event.destroy
+
+ respond_to do |format|
+ format.html { redirect_to backend_events_url }
+ format.json { head :no_content }
+ end
+ end
+end
@@ -6,6 +6,7 @@ class Backend::PostsController < BackendController
def find_post
@post = Post.find(params[:id])
end
+
protected :find_post
# GET /posts
@@ -33,6 +34,9 @@ def show
def new
@post = Post.new
@post.default_images
+ if params[:with_events]
+ @post.build_event
+ end
respond_to do |format|
format.html # new.html.erb
@@ -4,5 +4,7 @@ class MgzineController < ApplicationController
def index
@posts = PostDecorator.all
+
+ @events = EventDecorator.decorate Event.next_events
end
end
@@ -0,0 +1,42 @@
+class EventDecorator < ApplicationDecorator
+ decorates :Event
+
+ def linked_title
+ h.link_to model.title, model.post;
+ end
+
+ def start_date
+ if model.start_date.present?
+ h.render partial: 'events/start_date', locals: { start_date: model.start_date }
+ end
+ end
+
+ # Accessing Helpers
+ # You can access any helper via a proxy
+ #
+ # Normal Usage: helpers.number_to_currency(2)
+ # Abbreviated : h.number_to_currency(2)
+ #
+ # Or, optionally enable "lazy helpers" by including this module:
+ # include Draper::LazyHelpers
+ # Then use the helpers with no proxy:
+ # number_to_currency(2)
+
+ # Defining an Interface
+ # Control access to the wrapped subject's methods using one of the following:
+ #
+ # To allow only the listed methods (whitelist):
+ # allows :method1, :method2
+ #
+ # To allow everything except the listed methods (blacklist):
+ # denies :method1, :method2
+
+ # Presentation Methods
+ # Define your own instance methods, even overriding accessors
+ # generated by ActiveRecord:
+ #
+ # def created_at
+ # h.content_tag :span, time.strftime("%a %m/%d/%y"),
+ # :class => 'timestamp'
+ # end
+end
View
@@ -0,0 +1,14 @@
+class Event < ActiveRecord::Base
+ validates :title, presence: true
+
+ attr_accessible :end_date, :start_date, :title, :url
+
+ belongs_to :post, :inverse_of => :event
+
+ def self.next_events
+ next_seven_days = Time.now.beginning_of_day .. Time.now.beginning_of_day + 7.day
+
+ Event.order('start_date')
+ .where(:start_date => next_seven_days)
+ end
+end
View
@@ -9,6 +9,10 @@ class Post < ActiveRecord::Base
belongs_to :category
+ has_one :event, :inverse_of => :post
+
+ accepts_nested_attributes_for :event
+
accepts_nested_attributes_for :images,
reject_if: proc { |at| at['resource'].blank? }
@@ -5,14 +5,21 @@
multipart: true }) do |f| %>
<%= f.input :title %>
<%= f.input :body %>
- <%= f.input :excerpt %>
+ <%= f.input :excerpt, :as => :text %>
- <%= collection_select :post, :category_id, Category.all, :id, :name %>
+ <%= f.association :category %>
+
+ <% if @post.event.present? %>
+ <%= f.simple_fields_for :event do |fe| %>
+ <fieldset>
+ <legend>Datos del evento</legend>
+
+ <%= fe.input :title %>
+ <%= fe.input :start_date %>
+ </fieldset>
+ <% end %>
+ <% end %>
- <%#
- TODO: use bootstrap datepicker for the date
- @link: https://github.com/eternicode/bootstrap-datepicker
- %>
<%= f.input :published_at %>
<%= f.simple_fields_for(:images) do |fi| %>
@@ -20,7 +20,7 @@
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= post.excerpt %></td>
- <td><%= l post.published_at, format: :short %></td>
+ <td><%= l post.published_at, format: :short if post.published_at? %></td>
<td><%= post.published %></td>
<td><%= link_to 'Display', post %></td>
<td><%= link_to 'View', backend_post_path(post) %></td>
@@ -35,5 +35,6 @@
<br />
<%= link_to 'New Post', new_backend_post_path %>
+ <%= link_to 'New event post', new_backend_post_path(:with_events => 1), {}, %>
</div>
</div>
@@ -3,6 +3,5 @@
<li class="active">
<%= link_to 'Posts', backend_posts_path %>
</li>
- <li><a href="#">Events</a></li>
</ul>
<!-- /tabs -->
@@ -0,0 +1,12 @@
+<div itemscope itemtype="http://schema.org/Event">
+ <a itemprop="url" href="">
+ <span itemprop="name"><%= event.title %></span>
+
+ <%=
+ if event.start_date.present?
+ render :partial => 'events/start_date',
+ :locals => { :start_date => event.start_date }
+ end
+ %>
+ </a>
+</div>
@@ -0,0 +1,2 @@
+<meta itemprop="startDate" content="<%= start_date %>">
+<%= start_date %>
@@ -19,6 +19,8 @@
<%# TODO: consider to add modernizr %>
<%# javascript_include_tag "modernizr" %>
+
+ <%= stylesheet_link_tag 'bootstrap-datepicker/datepicker' %>
</head>
<body>
@@ -38,5 +40,13 @@
<%# NOTE: allways load the javascript at the bottom of the page %>
<%= javascript_include_tag 'backend' %>
+
+ <%= javascript_include_tag 'bootstrap-datepicker/bootstrap-datepicker' %>
+
+ <script type="text/Javascript">
+ $(document).ready( function() {
+ $('.datepicker').datepicker();
+ });
+ </script>
</body>
</htm
@@ -6,3 +6,11 @@
<%= post.category %>
</section>
<% end %>
+
+<h1>Agenda</h1>
+<% @events.each do |event| %>
+<section itemscope itemtype="http://schema.org/Event">
+ <h1><%= event.linked_title %></h1>
+ <%= event.start_date %>
+</section>
+<% end %>
@@ -11,6 +11,12 @@
<section class="body">
<%= @post.body %>
</section>
+
+ <%=
+ if @post.event.present?
+ render @post.event
+ end
+ %>
<!-- TODO -->
@@ -0,0 +1,12 @@
+class CreateEvents < ActiveRecord::Migration
+ def change
+ create_table :events do |t|
+ t.string :title
+ t.string :url
+ t.date :start_date
+ t.date :end_date
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,5 @@
+class AddPostToEvent < ActiveRecord::Migration
+ def change
+ add_column :events, :post_id, :integer
+ end
+end
View
@@ -11,14 +11,24 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120416214952) do
+ActiveRecord::Schema.define(:version => 20120418224035) do
create_table "categories", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
+ create_table "events", :force => true do |t|
+ t.string "title"
+ t.string "url"
+ t.date "start_date"
+ t.date "end_date"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "post_id"
+ end
+
create_table "images", :force => true do |t|
t.string "title"
t.string "resource"
View
@@ -1,3 +1,5 @@
+# encoding: utf-8
+#
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
@@ -41,5 +43,5 @@ def info(message)
# TODO: this should be improved as previously seen
Category.delete_all
-Category.create(name: "Música")
-Category.create(name: "Cine")
+Category.create(name: 'Música')
+Category.create(name: 'Cine')
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe EventDecorator do
+ before { ApplicationController.new.set_current_view_context }
+end
View
@@ -0,0 +1,10 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :event do
+ title "MyString"
+ url "MyString"
+ start_date "2012-04-18"
+ end_date "2012-04-18"
+ end
+end
View
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class EventTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
Oops, something went wrong.

0 comments on commit 96986b4

Please sign in to comment.