Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Got editable file upload working

  • Loading branch information...
commit e08adc3660cd9d1ca5544ff3d12631d302c301c8 1 parent 5c65142
@alexsanford alexsanford authored
View
2  app/presenters/locomotive/base_presenter.rb
@@ -50,7 +50,7 @@ def assign_attributes(new_attributes)
# Only call the methods which the presenter can handle
meth = :"#{attr}="
if self.respond_to? meth
- self.send(:"#{attr}=", value)
+ self.send(meth, value)
end
end
end
View
6 app/presenters/locomotive/editable_file_presenter.rb
@@ -3,8 +3,6 @@ class EditableFilePresenter < EditableElementPresenter
delegate :content, :to => :source
- delegate :content=, :to => :source
-
def filename
File.basename(self.content)
end
@@ -13,6 +11,10 @@ def url
self.content
end
+ def source=(value)
+ self.source.source = value
+ end
+
def included_methods
super + %w(filename content url)
end
View
47 app/presenters/locomotive/page_presenter.rb
@@ -5,6 +5,8 @@ class PagePresenter < BasePresenter
delegate :title=, :slug=, :fullpath=, :handle=, :raw_template=, :published=, :listed=, :templatized=, :templatized_from_parent=, :redirect=, :redirect_url=, :cache_strategy=, :response_type=, :to => :source
+ attr_writer :editable_elements
+
def escaped_raw_template
h(self.source.raw_template)
end
@@ -13,28 +15,28 @@ def editable_elements
self.source.enabled_editable_elements.collect(&:as_json)
end
- def editable_elements=(editable_elements)
- editable_elements.each do |editable_element_hash|
- editable_element_id = editable_element_hash[:id]
- editable_element_type = editable_element_hash[:type]
-
- # Check to see if the object exists in the DB
- if editable_element_id
- begin
- editable_element = self.source.editable_elements.find(editable_element_id)
- rescue ::Mongoid::Errors::DocumentNotFound
- editable_element = nil
- end
- end
+ def set_editable_elements
+ return unless @editable_elements
- # Create element if needed
- unless editable_element
- editable_element = "Locomotive::#{editable_element_type}".constantize.new
- editable_element.page = self.source
- end
+ # Need to parse to get the right editable elements first
+ self.source.send(:_parse_and_serialize_template)
+
+ @editable_elements.each do |editable_element_hash|
+ slug = editable_element_hash['slug']
+ block = editable_element_hash['block']
+
+ block = nil if block && block.empty?
- editable_element_presenter = editable_element.to_presenter
- editable_element_presenter.assign_attributes(editable_element_hash)
+ el = self.source.find_editable_element(block, slug)
+ el.to_presenter.assign_attributes(editable_element_hash) if el
+
+ # FIXME: Not sure why we need to do this...
+ if el.respond_to?(:source)
+ self.source.save
+ self.source.reload
+ el = self.source.find_editable_element(block, slug)
+ el.source = editable_element_hash['source']
+ end
end
end
@@ -65,5 +67,10 @@ def as_json_for_html_view
self.as_json(methods)
end
+ def save
+ set_editable_elements
+ super
+ end
+
end
end
View
87 features/api/editable_elements.feature
@@ -29,11 +29,13 @@ Feature: Editable Elements
"page": {
"editable_elements": [
{
- "id": %{LONG_TEXT_ID},
+ "slug": "welcome",
+ "block": null,
"content": "My new welcome content!!!"
},
{
- "id": %{SHORT_TEXT_ID},
+ "slug": "subtitle",
+ "block": "main",
"content": "My new main content!!!"
}
]
@@ -112,3 +114,84 @@ Feature: Editable Elements
| 3/editable_elements/1/slug | "my_long_text" |
| 3/editable_elements/1/block | "main" |
| 3/editable_elements/1/content | "<p>The new long text content</p>" |
+
+ Scenario: Create new editable file on new page
+ Given the JSON request at "page/editable_elements/0/source" is a file
+ When I do an API POST to pages.json with:
+ """
+ {
+ "page": {
+ "title": "New Page",
+ "parent_fullpath": "index",
+ "raw_template": "{% block main %}{% editable_file my_file %}{% endeditable_file %}{% endblock %}",
+ "editable_elements": [
+ {
+ "slug": "my_file",
+ "block": "main",
+ "type": "EditableFile",
+ "source": "images/logo2.jpg"
+ }
+ ]
+ }
+ }
+ """
+ When I do an API GET request to pages.json
+ Then the JSON response at "3/editable_elements" should have 1 entry
+ And the JSON response should have the following:
+ | 3/editable_elements/0/slug | "my_file" |
+ | 3/editable_elements/0/block | "main" |
+ And the JSON at "3/editable_elements/0/url" should match /logo2.jpg$/
+
+ Scenario: Create new editable file on existing page
+ Given the JSON request at "page/editable_elements/0/source" is a file
+ When I do an API PUT to pages/4f832c2cb0d86d3f42fffffe.json with:
+ """
+ {
+ "page": {
+ "title": "New Page",
+ "parent_fullpath": "index",
+ "raw_template": "{% block main %}{% editable_file my_file %}{% endeditable_file %}{% endblock %}",
+ "editable_elements": [
+ {
+ "slug": "my_file",
+ "block": "main",
+ "type": "EditableFile",
+ "source": "images/logo2.jpg"
+ }
+ ]
+ }
+ }
+ """
+ When I do an API GET request to pages/4f832c2cb0d86d3f42fffffe.json
+ Then the JSON response at "editable_elements" should have 1 entry
+ And the JSON response should have the following:
+ | editable_elements/0/slug | "my_file" |
+ | editable_elements/0/block | "main" |
+ And the JSON at "editable_elements/0/url" should match /logo2.jpg$/
+
+ Scenario: Create new editable file with no block
+ Given the JSON request at "page/editable_elements/0/source" is a file
+ When I do an API PUT to pages/4f832c2cb0d86d3f42fffffe.json with:
+ """
+ {
+ "page": {
+ "title": "New Page",
+ "parent_fullpath": "index",
+ "raw_template": "{% editable_file my_file %}{% endeditable_file %}",
+ "editable_elements": [
+ {
+ "slug": "my_file",
+ "block": null,
+ "type": "EditableFile",
+ "source": "images/logo2.jpg"
+ }
+ ]
+ }
+ }
+ """
+ When I do an API GET request to pages/4f832c2cb0d86d3f42fffffe.json
+ Then the JSON response at "editable_elements" should have 1 entry
+ And the JSON response should have the following:
+ | editable_elements/0/slug | "my_file" |
+ | editable_elements/0/block | null |
+ And the JSON at "editable_elements/0/url" should match /logo2.jpg$/
View
10 features/step_definitions/api_steps.rb
@@ -40,7 +40,9 @@ def add_file_field(json_path)
def get_field_at_path(hash, path)
val = hash
path.split('/').each do |key|
- if val.has_key? key
+ if val.is_a?(Array)
+ val = val[key.to_i]
+ elsif val.has_key?(key)
val = val[key]
else
return nil
@@ -54,7 +56,11 @@ def set_field_at_path(hash, path, value)
keys = path.split('/')
last_key = keys.slice!(-1)
keys.each do |key|
- obj = obj[key]
+ if obj.is_a?(Array)
+ obj = obj[key.to_i]
+ else
+ obj = obj[key]
+ end
end
obj[last_key] = value
end
Please sign in to comment.
Something went wrong with that request. Please try again.