Skip to content

Commit

Permalink
Revert "big cleaning and refactoring the presenters for the Locomotiv…
Browse files Browse the repository at this point in the history
…eCMS mounter module (work in progress)"

This reverts commit 0e502bf.
  • Loading branch information
bjorntrondsen committed Dec 20, 2012
1 parent afe3f5c commit 3604d61
Show file tree
Hide file tree
Showing 72 changed files with 597 additions and 1,192 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -13,7 +13,7 @@ group :development do

# gem 'locomotive-aloha-rails', :path => '../gems/aloha-rails' # for Developers
# gem 'locomotive-tinymce-rails', '~> 3.4.7.4', :path => '../gems/tinymce-rails' # for Developers
gem 'locomotive_liquid', :path => '../gems/liquid' # for Developers
# gem 'locomotive_liquid', :path => '../gems/liquid' # for Developers

gem 'rspec-rails', '~> 2.8.0' # In order to have rspec tasks and generators
gem 'rspec-cells'
Expand Down
9 changes: 2 additions & 7 deletions Gemfile.lock
Expand Up @@ -30,19 +30,14 @@ PATH
mongoid (~> 2.4.12)
multi_json (~> 1.3.4)
rack-cache (~> 1.1)
rails (~> 3.2.9)
rails (~> 3.2.8)
rails-backbone (~> 0.6.1)
rake (~> 0.9.2)
responders (~> 0.9.2)
rmagick (~> 2.12.2)
sanitize (~> 2.0.3)
unidecoder (~> 1.1.1)

PATH
remote: ../gems/liquid
specs:
locomotive_liquid (2.4.1)

GEM
remote: http://rubygems.org/
specs:
Expand Down Expand Up @@ -205,6 +200,7 @@ GEM
mongoid (~> 2.0)
locomotive-tinymce-rails (3.4.7.4)
actionpack (~> 3.2)
locomotive_liquid (2.4.1)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
Expand Down Expand Up @@ -338,7 +334,6 @@ DEPENDENCIES
json_spec
launchy
locomotive_cms!
locomotive_liquid!
mocha (= 0.9.12)
pickle
poltergeist (~> 1.0.2)
Expand Down
15 changes: 13 additions & 2 deletions app/controllers/locomotive/api/accounts_controller.rb
Expand Up @@ -19,9 +19,10 @@ def show
end

def create
@account = Locomotive::Account.from_presenter(params[:account])
@account = Locomotive::Account.new
authorize! :create, @account
@account.save
@account_presenter = @account.to_presenter
@account_presenter.update_attributes(params[:account])
respond_with(@account)
end

Expand All @@ -32,6 +33,16 @@ def destroy
respond_with(@account)
end

protected

def load_account
@account ||= load_accounts.find(params[:id])
end

def load_accounts
@accounts ||= current_site.accounts
end

end

end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/locomotive/api/content_assets_controller.rb
Expand Up @@ -16,15 +16,15 @@ def show

def create
@content_asset = current_site.content_assets.new
@content_asset.from_presenter(params[:content_asset])
@content_asset.save
@content_asset_presenter = @content_asset.to_presenter
@content_asset_presenter.update_attributes(params[:content_asset])
respond_with @content_asset, :location => main_app.locomotive_api_content_assets_url
end

def update
@content_asset = current_site.content_assets.find(params[:id])
@content_asset.from_presenter(params[:content_asset])
@content_asset.save
@content_asset_presenter = @content_asset.to_presenter
@content_asset_presenter.update_attributes(params[:content_asset])
respond_with @content_asset, :location => main_app.locomotive_api_content_assets_url
end

Expand Down
8 changes: 4 additions & 4 deletions app/controllers/locomotive/api/content_types_controller.rb
Expand Up @@ -16,15 +16,15 @@ def show

def create
@content_type = current_site.content_types.new
@content_type.from_presenter(params[:content_type])
@content_type.save
@content_type_presenter = @content_type.to_presenter
@content_type_presenter.update_attributes(params[:content_type])
respond_with @content_type, :location => main_app.locomotive_api_content_types_url
end

def update
@content_type = current_site.content_types.find(params[:id])
@content_type.from_presenter(params[:content_type])
@content_type.save
@content_type_presenter = @content_type.to_presenter
@content_type_presenter.update_attributes(params[:content_type])
respond_with @content_type, :location => main_app.locomotive_api_content_types_url
end

Expand Down
13 changes: 7 additions & 6 deletions app/controllers/locomotive/api/memberships_controller.rb
Expand Up @@ -3,8 +3,8 @@ module Api
class MembershipsController < BaseController

# It's an embedded document, so we'll just load manually
before_filter :load_membership, :only => [:show, :update, :destroy]
before_filter :load_memberships, :only => [:index]
before_filter :load_membership, :only => [:show, :update, :destroy]
before_filter :load_memberships, :only => [:index]

authorize_resource :class => Locomotive::Membership

Expand All @@ -17,15 +17,16 @@ def show
end

def create
build_params = params[:membership].merge({ :role => 'author' }) # force author by default
@membership = current_site.memberships.new
@membership.from_presenter(params[:membership].merge(:role => 'author')) # force author by default
@membership.save
@membership_presenter = @membership.to_presenter
@membership_presenter.update_attributes(build_params)
respond_with(@membership)
end

def update
@membership.from_presenter(params[:membership])
@membership.save
@membership_presenter = @membership.to_presenter
@membership_presenter.update_attributes(params[:membership])
respond_with(@membership)
end

Expand Down
8 changes: 4 additions & 4 deletions app/controllers/locomotive/api/pages_controller.rb
Expand Up @@ -16,15 +16,15 @@ def show

def create
@page = current_site.pages.new
@page.from_presenter(params[:page])
@page.save
@page_presenter = @page.to_presenter
@page_presenter.update_attributes(params[:page])
respond_with @page, :location => main_app.locomotive_api_pages_url
end

def update
@page = current_site.pages.find(params[:id])
@page.from_presenter(params[:page])
@page.save
@page_presenter = @page.to_presenter
@page_presenter.update_attributes(params[:page])
respond_with @page, :location => main_app.locomotive_api_pages_url
end

Expand Down
9 changes: 6 additions & 3 deletions app/controllers/locomotive/api/sites_controller.rb
Expand Up @@ -20,16 +20,19 @@ def show
end

def create
@site = Locomotive::Site.from_presenter(params[:site])
@site = Locomotive::Site.new
@site.memberships.build :account => self.current_locomotive_account, :role => 'admin'
@site.save
@site_presenter = @site.to_presenter
@site_presenter.update_attributes(params[:site])
respond_with(@site)
end

def update
@site = Locomotive::Site.find(params[:id])
@site.from_presenter(params[:site])
@site.save
authorize! :update, @site
@site_presenter = @site.to_presenter
@site_presenter.update_attributes(params[:site])
respond_with @site
end

Expand Down
8 changes: 4 additions & 4 deletions app/controllers/locomotive/api/snippets_controller.rb
Expand Up @@ -16,15 +16,15 @@ def show

def create
@snippet = current_site.snippets.new
@snippet.from_presenter(params[:snippet])
@snippet.save
@snippet_presenter = @snippet.to_presenter
@snippet_presenter.update_attributes(params[:snippet])
respond_with @snippet, :location => main_app.locomotive_api_snippets_url
end

def update
@snippet = current_site.snippets.find(params[:id])
@snippet.from_presenter(params[:snippet])
@snippet.save
@snippet_presenter = @snippet.to_presenter
@snippet_presenter.update_attributes(params[:snippet])
respond_with @snippet, :location => main_app.locomotive_api_snippets_url
end

Expand Down
9 changes: 4 additions & 5 deletions app/controllers/locomotive/api/theme_assets_controller.rb
@@ -1,7 +1,6 @@
module Locomotive
module Api
class ThemeAssetsController < BaseController

load_and_authorize_resource :class => Locomotive::ThemeAsset

def index
Expand All @@ -16,15 +15,15 @@ def show

def create
@theme_asset = current_site.theme_assets.new
@theme_asset.from_presenter(params[:theme_asset])
@theme_asset.save
@theme_asset_presenter = @theme_asset.to_presenter
@theme_asset_presenter.update_attributes(params[:theme_asset])
respond_with @theme_asset, :location => main_app.locomotive_api_theme_assets_url
end

def update
@theme_asset = current_site.theme_assets.find(params[:id])
@theme_asset.from_presenter(params[:theme_asset])
@theme_asset.save
@theme_asset_presenter = @theme_asset.to_presenter
@theme_asset_presenter.update_attributes(params[:theme_asset])
respond_with @theme_asset, :location => main_app.locomotive_api_theme_assets_url
end

Expand Down
1 change: 0 additions & 1 deletion app/controllers/locomotive/api/translations_controller.rb
@@ -1,7 +1,6 @@
module Locomotive
module Api
class TranslationsController < BaseController

load_and_authorize_resource :class => Locomotive::Translation, through: :current_site

def index
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/locomotive/custom_fields_helper.rb
Expand Up @@ -44,7 +44,7 @@ def options_for_text_formatting

def options_for_content_type
current_site.content_types.map do |c|
[c.name, c.entries_class_name]
[c.name, c.klass_with_custom_fields(:entries).to_s]
end.compact
end

Expand All @@ -58,7 +58,7 @@ def options_for_content_type_inverse_of
hash[type] << {
:label => field.label,
:name => field.name,
:class_name => content_type.entries_class_name
:class_name => content_type.klass_with_custom_fields(:entries).to_s
}
end
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/locomotive/account.rb
Expand Up @@ -95,6 +95,14 @@ def devise_mailer
Locomotive::DeviseMailer
end

def to_presenter(options = {})
Locomotive::AccountPresenter.new(self, options)
end

def as_json(options = {})
Locomotive::AccountPresenter.new(self, options).as_json
end

protected

def password_required?
Expand Down
11 changes: 10 additions & 1 deletion app/models/locomotive/content_asset.rb
@@ -1,7 +1,8 @@
module Locomotive
class ContentAsset

include Locomotive::Mongoid::Document
include ::Mongoid::Document
include ::Mongoid::Timestamps

## extensions ##
include Extensions::Asset::Types
Expand Down Expand Up @@ -36,5 +37,13 @@ def to_liquid
{ :url => self.source.url }.merge(self.attributes).stringify_keys
end

def to_presenter
Locomotive::ContentAssetPresenter.new(self)
end

def as_json(options = {})
self.to_presenter.as_json
end

end
end
16 changes: 14 additions & 2 deletions app/models/locomotive/content_entry.rb
Expand Up @@ -10,8 +10,8 @@ class ContentEntry
## fields ##
field :_slug
field :_label_field_name
field :_position, :type => Integer, :default => 0
field :_visible, :type => Boolean, :default => true
field :_position, :type => Integer, :default => 0
field :_visible, :type => Boolean, :default => true

## validations ##
validates :_slug, :presence => true, :uniqueness => { :scope => :content_type_id }
Expand Down Expand Up @@ -122,6 +122,18 @@ def self.sort_entries!(ids)
end
end

def to_liquid
Locomotive::Liquid::Drops::ContentEntry.new(self)
end

def to_presenter(options = {})
Locomotive::ContentEntryPresenter.new(self, options)
end

def as_json(options = {})
self.to_presenter(options).as_json
end

protected

# Retrieve the next or the previous entry following the order
Expand Down
32 changes: 9 additions & 23 deletions app/models/locomotive/content_type.rb
Expand Up @@ -71,7 +71,7 @@ def groupable?
end

def group_by_field
self.find_entries_custom_field(self.group_by_field_id)
self.entries_custom_fields.find(self.group_by_field_id) rescue nil
end

def list_or_group_entries
Expand Down Expand Up @@ -103,28 +103,6 @@ def label_field_name=(value)
super(value)
end

# Get the class name of the entries.
#
# @return [ String ] The class name of all the entries
#
def entries_class_name
self.klass_with_custom_fields(:entries).to_s
end

# Find a custom field describing an entry based on its id
# in first or its name if not found.
#
# @param [ String ] id_or_name The id of name of the field
#
# @return [ Object ] The custom field or nit if not found
#
def find_entries_custom_field(id_or_name)
return nil if id_or_name.nil? # bypass the memoization

_field = self.entries_custom_fields.find(id_or_name) rescue nil
_field || self.entries_custom_fields.where(:name => id_or_name).first
end

# Retrieve from a class name the associated content type within the scope of a site.
# If no content type is found, the method returns nil
#
Expand All @@ -141,6 +119,14 @@ def self.class_name_to_content_type(class_name, site)
end
end

def to_presenter
Locomotive::ContentTypePresenter.new(self)
end

def as_json(options = {})
self.to_presenter.as_json
end

protected

def group_by_belongs_to_field(field)
Expand Down
4 changes: 4 additions & 0 deletions app/models/locomotive/editable_control.rb
Expand Up @@ -31,6 +31,10 @@ def copy_attributes_from(el)
end
end

def to_presenter
Locomotive::EditableControlPresenter.new(self)
end

protected

def propagate_content
Expand Down

0 comments on commit 3604d61

Please sign in to comment.