Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing: cms_manageable #409

Merged
merged 15 commits into from
Feb 10, 2014
Merged

Introducing: cms_manageable #409

merged 15 commits into from
Feb 10, 2014

Conversation

Emerson
Copy link
Contributor

@Emerson Emerson commented Feb 7, 2014

cms_manageable

Have you ever been working on a project and wanted to treat one of your existing models just like a Cms::Page object?

At first this might sound bizarre, but I assure you, the use case is real, awesome, and actually has minimal impact on the comfy codebase. Let's take a moment to think this through....

Anyone who knows Comfy understands that its real benefit comes from layouts, tags, and blocks. Writing a layout that generates a dynamic form is a huge time saver and there have been a number of occasions where I've wanted the magic of Comfy mixed into my boring old Rails models. So with this in mind, I'd like to introduce cms_manageable.

What Does It Do?

Let's start with a boring old rails model:

class BoringModel < ActiveRecord::Base

  validates :is_boring, presence: true

end

So hey, let's spice things up by adding in the cms_manageable behaviour. The only requirements are that we provide a layout method that returns a proper Comfy layout and add a content column on the model. Once you've done this, the benefits are clear:

class BoringModel < ActiveRecord::Base

  validates :is_awesome, presence: true

  cms_manageable

  # Your job to figure out a nice way to handle this...
  def layout
    Cms::Layout.new(content: "<p>{{ cms:page:random_content }}</p>")
  end

end


# Let's assume we have an instance of our BoringModel
boring_model = BoringModel.first
# Set some blocks that match the layout we defined in the class...
boring_model.blocks_attributes = [{ :identifier => 'random_content', :content => 'not so boring anymore ;-)' }]

boring_model.render
#=> "<p>not so boring anymore ;-)</p>"

c'est bon, non?

But Why?

The possibilities are endless. Blocks. Layouts. Revisions. Rendering. Form Builders. Etc...

But How?

Cms::Blocks are now polymorphic. Mind == Blown.

What About Forms?

Ahhh, yes. Let's use some of those sweet sweet Comfy forms. Obviously the context of this code is within your admin area:

.row-fluid
  .span12
.page-header
  = comfy_form_for @awesome_model do |form|
    = form.text_field :title  
    = form.text_area :normal_boring_stuff
    / Bring the awesome...
    / renders the Comfy form builder and handles any blocks you have...
    / it's an engine, so just override it if you need anything special 
    = render partial: 'admin/cms/pages/form_blocks', locals: {cms_object: form.object}

@GBH
Copy link
Member

GBH commented Feb 7, 2014

Amazing. This calls for a 1.12.0 release.
There a few minor potential issues I need to check before merging.

Few things that spring to mind:

  • cms_has_revisions_for should not be part of the module. It only deals with blocks, if might want to version other attributes in your boring model.
  • ability to associate BoringModel with Cms::Layout is there, so we need to make sure that view helper to select layouts is available
  • I wonder how does Site segmentation comes into play here? Does it even matter?
  • upgrade Migration

@hesham
Copy link
Contributor

hesham commented Feb 7, 2014

This is fucking great! May I suggest another name for the extension? cms_manageable instead of cms_acts_as_cms.

@GBH
Copy link
Member

GBH commented Feb 7, 2014

  1. cms_powered
  2. cms_managed
  3. cms_enabled
  4. cms_enhanced
  5. cms_driven
  6. cms_boosted

@hesham
Copy link
Contributor

hesham commented Feb 7, 2014

@GBH
Copy link
Member

GBH commented Feb 7, 2014

@hesham
Copy link
Contributor

hesham commented Feb 7, 2014

it's not enterprisey enough

@GBH
Copy link
Member

GBH commented Feb 8, 2014

  1. cms_vertical_integration
  2. cms_leverage
  3. cms_paradigm
  4. cms_synergy
  5. cms_thought_leader

@Emerson
Copy link
Contributor Author

Emerson commented Feb 8, 2014

+1 cms_manageable

@Emerson
Copy link
Contributor Author

Emerson commented Feb 9, 2014

@GBH

  • Now using cms_manageable. It's much nicer sounding and makes good sense.
  • I've added an upgrade migration. I copied it over to my local Comfy project, ran it, and everything worked. You might want to take a quick look just in case, specifically around the naming of the migration (I'm not great with migrations).
  • I think the layouts should be fully handled by the host app. Let's see how my current app ends up. Perhaps I'll need this functionality anyways.
  • I can't see any reason that the changes I've made to blocks would affect Site segmentation, but you know this library better than me.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.02%) when pulling aee5a36 on Emerson:feature/acts_as_cms into 3a80f19 on comfy:master.

p = self.block.page
Cms::Page.where(:id => p.id).update_all(:content => nil)
p = self.block.blockable
p.class.name.constantize.where(:id => p.id).update_all(:content => nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p used to stand for page

@Emerson
Copy link
Contributor Author

Emerson commented Feb 10, 2014

@GBH - Just pushed some cleanup here.

GBH added a commit that referenced this pull request Feb 10, 2014
@GBH GBH merged commit 8080c47 into comfy:master Feb 10, 2014
@Emerson
Copy link
Contributor Author

Emerson commented Feb 10, 2014

Feels.

GBH added a commit that referenced this pull request Feb 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants