Permalink
Browse files

Merge pull request #3 from HangingClowns/master

Clean up of some previous code, unit tests for Owns (Possessions)
  • Loading branch information...
2 parents 8076e21 + 45a151b commit a0835aa7a527bba57f9cf2afae33b38455d7f112 @jmlagace jmlagace committed Mar 22, 2013
View
@@ -38,7 +38,7 @@ group :development, :test do
gem 'guard-rspec'
gem 'guard-spork', "~> 1.4.1"
gem 'rspec-rails'
- # gem 'mongoid-rspec'
+ gem 'mongoid-rspec'
gem 'database_cleaner'
gem 'therubyracer'
gem 'spork', "~> 1.0.0rc3"
View
@@ -103,7 +103,7 @@ GEM
factory_girl (~> 4.2.0)
railties (>= 3.0.0)
fast-stemmer (1.0.2)
- ffi (1.4.0)
+ ffi (1.5.0)
gherkin (2.11.6)
json (>= 1.7.6)
guard (1.6.2)
@@ -125,7 +125,7 @@ GEM
childprocess (>= 0.2.3)
guard (>= 1.1)
spork (>= 0.8.4)
- haml (4.0.0)
+ haml (4.0.1)
tilt
hike (1.2.1)
http_accept_language (1.0.2)
@@ -142,7 +142,7 @@ GEM
addressable (~> 2.3)
libv8 (3.11.8.13)
listen (0.7.3)
- lumberjack (1.0.2)
+ lumberjack (1.0.3)
mail (2.5.3)
i18n (>= 0.4.0)
mime-types (~> 1.16)
@@ -158,13 +158,17 @@ GEM
activesupport
easy_diff
mongoid (~> 3.0)
+ mongoid-rspec (1.7.0)
+ mongoid (>= 3.0.1)
+ rake
+ rspec (>= 2.9)
mongoid_search (0.3.1)
fast-stemmer (~> 1.0.0)
mongoid (>= 3.0.0)
moped (1.4.3)
multi_json (1.7.1)
- newrelic_rpm (3.5.8.72)
- nokogiri (1.5.7)
+ newrelic_rpm (3.6.0.78)
+ nokogiri (1.5.9)
open4 (1.3.0)
origin (1.0.11)
orm_adapter (0.4.0)
@@ -201,7 +205,7 @@ GEM
ffi (>= 0.5.0)
rdoc (3.12.2)
json (~> 1.4)
- ref (1.0.2)
+ ref (1.0.4)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
@@ -234,7 +238,7 @@ GEM
therubyracer (0.11.4)
libv8 (~> 3.11.8.12)
ref
- thin (1.5.0)
+ thin (1.5.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
@@ -281,6 +285,7 @@ DEPENDENCIES
launchy
mongoid (~> 3.0.0)
mongoid-history
+ mongoid-rspec
mongoid_search
newrelic_rpm
rails (= 3.2.13)
View
@@ -1,13 +1,13 @@
-guard 'bundler' do
+guard 'bundler', cli: "--verbose" do
watch('Gemfile')
# Uncomment next line if Gemfile contain `gemspec' command
# watch(/^.+\.gemspec/)
end
-# guard 'rails' do
-# watch('Gemfile.lock')
-# watch(%r{^(config|lib)/.*})
-# end
+guard 'rails' do
+ watch('Gemfile.lock')
+ watch(%r{^(config|lib)/.*})
+end
guard 'spork', cucumber_env: { 'RAILS_ENV' => 'test' }, rspec_env: { 'RAILS_ENV' => 'test' }, test_unit: false, wait: 90 do
watch('config/application.rb')
@@ -0,0 +1,2 @@
+// Place all the behaviors and hooks related to the matching controller here.
+// All this logic will automatically be available in application.js.
@@ -0,0 +1,4 @@
+/*
+ Place all the styles related to the matching controller here.
+ They will automatically be included in application.css.
+*/
@@ -0,0 +1,91 @@
+class PossessionsController < ApplicationController
+ before_filter :authenticate_user!
+ before_filter :find_possession, only: [:show, :edit, :destroy, :update]
+ # GET /possessions
+ # GET /possessions.json
+ def index
+ @possessions = current_user.possessions
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @possessions }
+ end
+ end
+
+ # GET /possessions/1
+ # GET /possessions/1.json
+ def show
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @possession }
+ end
+ end
+
+ # GET /possessions/new
+ # GET /possessions/new.json
+ def new
+ @possession = current_user.possessions.build
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @possession }
+ end
+ end
+
+ # GET /possessions/1/edit
+ def edit
+ end
+
+ # POST /possessions
+ # POST /possessions.json
+ def create
+ @possession = current_user.possessions.build(params[:possession])
+
+ respond_to do |format|
+ if @possession.save
+ format.html { redirect_to @possession, notice: 'Possession was successfully created.' }
+ format.json { render json: @possession, status: :created, location: @possession }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @possession.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /possessions/1
+ # PUT /possessions/1.json
+ def update
+
+ respond_to do |format|
+ if @possession.update_attributes(params[:possession])
+ format.html { redirect_to @possession, notice: 'Possession was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @possession.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /possessions/1
+ # DELETE /possessions/1.json
+ def destroy
+ @possession.destroy
+
+ respond_to do |format|
+ format.html { redirect_to possessions_url }
+ format.json { head :no_content }
+ end
+ end
+
+ private
+
+ def find_possession
+ begin
+ @possession = current_user.possessions.find(params[:id])
+ rescue Mongoid::Errors::DocumentNotFound
+ redirect_to possessions_url
+ end
+ end
+
+end
@@ -0,0 +1,2 @@
+module PossessionsHelper
+end
View
@@ -4,47 +4,49 @@ class Album
include Mongoid::Search
include Mongoid::History::Trackable
- field :title, :type => String
- field :date_released, :type => IncDate
- field :label, :type => String
- field :media_type, :type => String
- field :reference_code, :type => String
- field :number_of_recordings, :type => Integer
- field :origalbumid, :type => String
+ field :title, type: String
+ field :date_released, type: IncDate
+ field :label, type: String
+ field :media_type, type: String
+ field :reference_code, type: String
+ field :number_of_recordings, type: Integer
+ field :origalbumid, type: String
#
# calculated values so we can index and sort
#
- field :cache_normalized_title, :type => String, :default => ""
- field :cache_first_letter, :type => String, :default => ""
+ field :cache_normalized_title, type: String, default: ""
+ field :cache_first_letter, type: String, default: ""
before_save :update_cached_fields
index({ cache_normalized_title: 1 }, { background: true })
index({ cache_first_letter: 1, cache_normalized_title: 1 }, { background: true })
- search_in :title, :label, {:match => :all}
+ search_in :title, :label, {match: :all}
validates_presence_of :title
- embeds_many :artist_wiki_links, :as=>:linkable, :class_name=>"AlbumArtistWikiLink"
+ embeds_many :artist_wiki_links, as: :linkable, class_name: "AlbumArtistWikiLink"
accepts_nested_attributes_for :artist_wiki_links
validates_associated :artist_wiki_links
- embeds_many :recording_wiki_links, :as=>:linkable, :class_name=>"AlbumRecordingWikiLink"
+ embeds_many :recording_wiki_links, as: :linkable, class_name: "AlbumRecordingWikiLink"
accepts_nested_attributes_for :recording_wiki_links
validates_associated :recording_wiki_links
- embeds_many :supplementary_sections, :class_name=>"SupplementarySection"
+ embeds_many :supplementary_sections, class_name: "SupplementarySection"
accepts_nested_attributes_for :supplementary_sections
validates_associated :supplementary_sections
+
+ has_many :possessions
# telling Mongoid::History how you want to track changes
- track_history :modifier_field => :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
- :version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
- :track_create => true, # track document creation, default is false
- :track_update => true, # track document updates, default is true
- :track_destroy => true # track document destruction, default is false
+ track_history modifier_field: :modifier, # adds "referenced_in :modifier" to track who made the change, default is :modifier
+ version_field: :version, # adds "field :version, :type => Integer" to track current version, default is :version
+ track_create: true, # track document creation, default is false
+ track_update: true, # track document updates, default is true
+ track_destroy: true # track document destruction, default is false
def artist_wiki_links_text
@@ -58,7 +60,7 @@ def artist_wiki_links_combined_links
def artist_wiki_links_text=(value)
self.artist_wiki_links.reverse.each{|a| a.destroy} #TODO find a way to do it at large since the self.artist_wiki_links.clear does not work
value.split(",").each{|q|
- self.artist_wiki_links.build(:reference_text=>q.strip)
+ self.artist_wiki_links.build(reference_text: q.strip)
}
end
@@ -75,7 +77,7 @@ def recording_wiki_links_combined_links
end
def recording_wiki_links_combined_links_renamed
- mappings = {:title => :name}
+ mappings = {title: :name}
recording_wiki_links_combined_links.collect do |x|
Hash[x.map {|k,v| [mappings[k] || k, v] }]
end
@@ -84,7 +86,7 @@ def recording_wiki_links_combined_links_renamed
def recording_wiki_links_text=(value)
self.recording_wiki_links.reverse.each{|a| a.destroy} #TODO find a way to do it at large since the self.recording_wiki_links.clear does not work
value.split(",").each{|q|
- self.recording_wiki_links.build(:reference_text=>q.strip)
+ self.recording_wiki_links.build(reference_text: q.strip)
}
end
@@ -114,7 +116,7 @@ def update_cached_fields
end
def to_wiki_link
- AlbumWikiLink.new(:reference_text=>"oid:#{self.id}", :album=>self)
+ AlbumWikiLink.new(reference_text: "oid:#{self.id}", album: self)
end
scope :queried, ->(q) {
View
@@ -0,0 +1,6 @@
+class Possession
+ include Mongoid::Document
+
+ belongs_to :owner, class_name: "User", index: true
+ belongs_to :album, index: true
+end
View
@@ -6,22 +6,22 @@ class User
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
- field :email, :type => String#, null: false
- field :encrypted_password, :type => String#, null: false
+ field :email, type: String#, null: false
+ field :encrypted_password, type: String#, null: false
## Recoverable
- field :reset_password_token, :type => String
- field :reset_password_sent_at, :type => Time
+ field :reset_password_token, type: String
+ field :reset_password_sent_at, type: Time
## Rememberable
- field :remember_created_at, :type => Time
+ field :remember_created_at, type: Time
## Trackable
- field :sign_in_count, :type => Integer
- field :current_sign_in_at, :type => Time
- field :last_sign_in_at, :type => Time
- field :current_sign_in_ip, :type => String
- field :last_sign_in_ip, :type => String
+ field :sign_in_count, type: Integer
+ field :current_sign_in_at, type: Time
+ field :last_sign_in_at, type: Time
+ field :current_sign_in_ip, type: String
+ field :last_sign_in_ip, type: String
## Encryptable
# field :password_salt, :type => String
@@ -48,11 +48,13 @@ class User
validates_uniqueness_of :nickname
validates_presence_of :nickname
- field :groups, :type=>Array, :default=>["user"]
+ field :groups, type: Array, default: ["user"]
- embeds_many :album_wiki_links, :as=>:linkable
+ embeds_many :album_wiki_links, as: :linkable
accepts_nested_attributes_for :album_wiki_links
validates_associated :album_wiki_links
+
+ has_many :possessions, foreign_key: "owner_id"
def album_wiki_links_text
album_wiki_links.collect{|v| v.reference_text }.join(",")
@@ -0,0 +1,17 @@
+<%= form_for(@possession) do |f| %>
+ <% if @possession.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@possession.errors.count, "error") %> prohibited this possession from being saved:</h2>
+
+ <ul>
+ <% @possession.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing possession</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @possession %> |
+<%= link_to 'Back', possessions_path %>
Oops, something went wrong.

0 comments on commit a0835aa

Please sign in to comment.